我想问一下使用数据库连接和关闭它们的常用方法是什么.
这是我的程序,但我在一个例子中看到,connection.Close()将不会执行.
我应该在整个街区使用try-catch吗?因为某种原因,我看到大多数人都没有.
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand())
{
command.CommandText = "procedure";
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
tmpParameter = DBUtils.createInSQLParam("@ImageID", SqlDbType.SmallInt, htmlImageId);
command.Parameters.Add(tmpParameter);
command.Connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
htmlImageDetails = GetHtmlImageDetailsFromReader(reader, true, out newId);
reader.Close();
}
connection.Close();
return htmlImageDetails;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1015 次 |
| 最近记录: |