相关疑难解决方法(0)

在"使用"块中是SqlConnection在返回或异常时关闭?

第一个问题:
说我有

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();

    string storedProc = "GetData";
    SqlCommand command = new SqlCommand(storedProc, connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));

    return (byte[])command.ExecuteScalar();
}
Run Code Online (Sandbox Code Playgroud)

连接是否关闭?因为从技术上讲,我们永远不会}return以前那样到达最后.

第二个问题:
这次我有:

try
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        int employeeID = findEmployeeID();

        connection.Open();
        SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
        command.CommandTimeout = 5;

        command.ExecuteNonQuery();
    }
}
catch (Exception) { /*Handle error*/ }
Run Code Online (Sandbox Code Playgroud)

现在,说出来try我们得到一个错误,它被抓住了.连接是否仍然关闭?因为我们再次跳过其余代码try并直接转到catch …

c# using sqlconnection

134
推荐指数
5
解决办法
16万
查看次数

标签 统计

c# ×1

sqlconnection ×1

using ×1