第一个问题:
假设我有
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();
}
连接会关闭吗?因为从技术上讲,我们永远不会像在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*/ }
现在,假设我们在try
的某个地方遇到一个错误,它被捕获了。连接是否仍处于关闭状态?因为我们再次跳过try
中的其余代码,直接转到catch
语句。
在using
的工作原理上,我是不是想得太简单了?ie当我们离开using
作用域时,Dispose()
会被简单地调用吗?
发布于 2011-01-18 04:56:54
无论哪种方式,当using块退出时(无论是由于成功完成还是由于错误),它都会关闭。
尽管我认为这样组织会更好,因为它更容易看到将要发生的事情,即使是对以后支持它的新维护程序员也是如此:
using (SqlConnection connection = new SqlConnection(connectionString))
{
int employeeID = findEmployeeID();
try
{
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*/
}
}
发布于 2011-01-18 05:04:01
这两个问题都是肯定的。using语句被编译成try/finally块
using (SqlConnection connection = new SqlConnection(connectionString))
{
}
等同于
SqlConnection connection = null;
try
{
connection = new SqlConnection(connectionString);
}
finally
{
if(connection != null)
((IDisposable)connection).Dispose();
}
编辑:将造型固定为可丢弃的http://msdn.microsoft.com/en-us/library/yh598w02.aspx
发布于 2013-08-09 21:07:23
这是我的模板。从SQL服务器中选择数据所需的一切。关闭并释放连接,并捕获连接和执行中的错误。
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["CompanyServer"].ConnectionString;
string selectStatement = @"
SELECT TOP 1 Person
FROM CorporateOffice
WHERE HeadUpAss = 1 AND Title LIKE 'C-Level%'
ORDER BY IntelligenceQuotient DESC
";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(selectStatement, conn))
{
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
{
if (dr.HasRows)
{
while (dr.Read())
{
Console.WriteLine(dr["Person"].ToString());
}
}
else Console.WriteLine("No C-Level with Head Up Ass Found!? (Very Odd)");
}
}
catch (Exception e) { Console.WriteLine("Error: " + e.Message); }
if (conn.State == System.Data.ConnectionState.Open) conn.Close();
}
}
*审校: 2015-11-09 *
正如NickG所建议的;如果太多的花括号让你感到厌烦,格式如下...
using (SqlConnection conn = new SqlConnection(connString))
using (SqlCommand comm = new SqlCommand(selectStatement, conn))
{
try
{
conn.Open();
using (SqlDataReader dr = comm.ExecuteReader())
if (dr.HasRows)
while (dr.Read()) Console.WriteLine(dr["Person"].ToString());
else Console.WriteLine("No C-Level with Head Up Ass Found!? (Very Odd)");
}
catch (Exception e) { Console.WriteLine("Error: " + e.Message); }
if (conn.State == System.Data.ConnectionState.Open) conn.Close();
}
话又说回来,如果你在EA或DayBreak游戏公司工作,你也可以放弃任何换行符,因为这些换行符只适用于那些以后必须回来查看你的代码的人,谁会真的在乎呢?我说的对吗?我的意思是一行而不是23行意味着我是一个更好的程序员,对吧?
using (SqlConnection conn = new SqlConnection(connString)) using (SqlCommand comm = new SqlCommand(selectStatement, conn)) { try { conn.Open(); using (SqlDataReader dr = comm.ExecuteReader()) if (dr.HasRows) while (dr.Read()) Console.WriteLine(dr["Person"].ToString()); else Console.WriteLine("No C-Level with Head Up Ass Found!? (Very Odd)"); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } if (conn.State == System.Data.ConnectionState.Open) conn.Close(); }
嘿嘿。好的。我把它从我的系统中拿出来,玩了一段时间。继续吧。
https://stackoverflow.com/questions/4717789
复制相似问题