Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#SQL连接、命令和使用语句-是否显式关闭和处置?_C#_Sql_Sql Server_Garbage Collection_Connection Timeout - Fatal编程技术网

C#SQL连接、命令和使用语句-是否显式关闭和处置?

C#SQL连接、命令和使用语句-是否显式关闭和处置?,c#,sql,sql-server,garbage-collection,connection-timeout,C#,Sql,Sql Server,Garbage Collection,Connection Timeout,当IDisposable对象与Using子句一起使用时,我读到了关于显式关闭和/或处理IDisposable对象的冲突信息 据我所知: using (x) { .... } 编译时,将被重写为: try { .... } finally { if (x != null) x.Dispose(); } 这意味着在using块的末尾立即调用Dispose,对吗 有些人甚至在使用using子句时也建议显式调用close和/或DISPLACE,因为等待最终块执行可能会有一些延迟 其他人说调用Disp

当IDisposable对象与Using子句一起使用时,我读到了关于显式关闭和/或处理IDisposable对象的冲突信息

据我所知:

using (x) { .... }
编译时,将被重写为:

try { .... } finally { if (x != null) x.Dispose(); }
这意味着在using块的末尾立即调用Dispose,对吗

有些人甚至在使用using子句时也建议显式调用close和/或DISPLACE,因为等待最终块执行可能会有一些延迟

其他人说调用Dispose在Using块中总是多余的。我倾向于同意,但我正在寻找一个明确的答案

垃圾收集(GC)是否只有在未使用Using子句且未显式关闭和处理时才起作用

以下面的方法为例(参见注释)


您正确描述了在块末尾使用调用的Dispose方法。

在随机时间对自定义对象的GC调用Finalize()方法(直接处置()。

通常Close方法包含在Dispose方法中,它不需要调用这两者,您必须阅读具体类的文档

在你的情况下,我会将代码更改为

 public Musician GetMusician(int recordId)
 {
     Musician objMusician = null;


     using(SqlConnection con = new SqlConnection(_connectionString))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "selectMusician";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", recordId);

                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        objMusician = new Musician((int) reader["id"]);
                        objMusician.Name = (string) reader["name"];
                    }

                    if objMusician != null)
                    {
                        objMusician.Albums = Albums.GetAlbums((int)objMusician.ID);
                        objMusician.Tours = Tours.GetTours((int)objMusician.ID);
                        objMusician.Interviews = Interviews.GetInterviews((int)objMusician.ID);
                    }
                }
            }
        }    
    return objMusician;
}

您正确描述了在块末尾使用调用的Dispose方法。

在随机时间对自定义对象的GC调用Finalize()方法(直接处置()。

通常Close方法包含在Dispose方法中,它不需要调用这两者,您必须阅读具体类的文档

在你的情况下,我会将代码更改为

 public Musician GetMusician(int recordId)
 {
     Musician objMusician = null;


     using(SqlConnection con = new SqlConnection(_connectionString))
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "selectMusician";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", recordId);

                using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        objMusician = new Musician((int) reader["id"]);
                        objMusician.Name = (string) reader["name"];
                    }

                    if objMusician != null)
                    {
                        objMusician.Albums = Albums.GetAlbums((int)objMusician.ID);
                        objMusician.Tours = Tours.GetTours((int)objMusician.ID);
                        objMusician.Interviews = Interviews.GetInterviews((int)objMusician.ID);
                    }
                }
            }
        }    
    return objMusician;
}

在使用块之后,
SqlCommand
将被释放,但GC将收集它。除非您显式添加延迟,否则程序到达块时,最终块将直接执行。在使用块之后,
SqlCommand
将被释放,但GC将收集它。除非您显式添加延迟,最后,当程序到达块时,直接执行块。