Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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# 通过使用报表清理资源&;为DataTable、SqlConnection、SqlCommand和;数据适配器_C#_Ado.net_Resources_Using_Sqlclient - Fatal编程技术网

C# 通过使用报表清理资源&;为DataTable、SqlConnection、SqlCommand和;数据适配器

C# 通过使用报表清理资源&;为DataTable、SqlConnection、SqlCommand和;数据适配器,c#,ado.net,resources,using,sqlclient,C#,Ado.net,Resources,Using,Sqlclient,我必须调用一个存储过程并得到结果。我知道有多种方法可以做到这一点(就像所有编程一样),我应该通过调用Dispose()和/或Close()来清理资源。读到这个问题,我想我应该使用using语句,这就足够了。下面是我打电话的方式。我的问题是——我是否把这件事复杂化了,这会清理掉所有的资源 private Int32 CallStoredProcedure(Int32 Id) { using (var dt = new DataTable()) { using (va

我必须调用一个存储过程并得到结果。我知道有多种方法可以做到这一点(就像所有编程一样),我应该通过调用
Dispose()
和/或
Close()
来清理资源。读到这个问题,我想我应该使用
using
语句,这就足够了。下面是我打电话的方式。我的问题是——我是否把这件事复杂化了,这会清理掉所有的资源

private Int32 CallStoredProcedure(Int32 Id)
{
    using (var dt = new DataTable())
    {
        using (var conn = new SqlConnection(ConnectionString))
        {
            using (var sqlCmd = new SqlCommand("SEL_StoredProcedure", conn))
            {
                using (var sda = new SqlDataAdapter(sqlCmd))
                {
                    sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                    sqlCmd.Parameters.AddWithValue("@ID", Id);
                    sqlCmd.Connection.Open();

                    sda.Fill(dt);
                }
            }
        }

        if (dt.Rows.Count == 1)
            return Convert.ToInt32(dt.Rows[0]["IDv2"]);
        else if (dt.Rows.Count > 1)
            throw new Exception("Multiple records were found with supplied ID; ID = " + studentId.ToString());
    }
    return 0;
}

另外,我知道我可以调用ExecuteScalar,但这不是我在这里要找的,因为我将使用非标量调用的通用格式。

AFAIK您不需要使用块包装DataTable或SqlDataAdapter,因为它们不实现IDisposable

您可以像下面这样“链接”using语句:

using(var conn = new SqlConnection(connectionString))
using(var cmd = new SqlCommand("SEL_storedProcedure", conn))
{

}

您编写的代码似乎可以正确地处理所有对象


您应该注意的是,处理数据表会使该对象不可用,这通常不是数据表的目的。通常,如果要填充数据表,您希望将数据保留(缓存)一段时间,而不是在查询方法中丢弃数据。

是的。它们分别继承可丢弃的
MarshallByValueComponent
Component
。链接很有趣,将删除许多大括号。不过Chris是对的,他们都有intellisense中的
Dispose()
方法:-)很高兴知道,谢谢。我很少看到DataTable在使用中被包装(我自己也很少这样做)或在使用后被丢弃。@RobinHames如果您感兴趣,有一个代码分析检查来确保在任何需要的地方调用Dispose()。CA1063。