文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>asp.net 使用using代码块 摘

asp.net 使用using代码块 摘

时间:2011-03-19  来源:ndxsdhy

摘自《ADO.NET 2.0 技术内幕》

"

强烈建议在Using代码块内所有可能的位置创建短期生存对象。

Using 块确保及时在Using块内波发生未处理异常时, 也会在该代码块的末尾调用 Dispose 方法。

 

例子:

string strConn, strSQL;
strconn
= @"Data Source=.\SQLExpress;" + "Initial Catalog = Northwind;Integrated Security=True;";
strSQL
= "SELECT CustomerID, CompanyName FROM Customers";
Using (SqlConnection cn
= new SqlConnection(strConn)) {
Try {
cn.Open();
}
Catch (SqlException ex) {
Consolse.WriteLine(
"Connect attempt failed");
Consolse.WriteLine(
" {0}", ex.Message);
return;
}

Using (SqlCommand cmd
= new SqlCommand(strSQL, cn) {
try {
using (SqlDataReader rdr = cmd.ExecuteReader()) {
while (rdr.Read())
Console.WriteLine(rdr[
"CompanyName"]);
rdr.Close();
}
}
catch (SqlException ex) {
Console.WriteLine(
"Query failed");
Console.WriteLine(
"{0}", ex.Message);
return;
}
}
cn.Close();
}
"
相关阅读 更多 +
排行榜 更多 +
涂色我贼强安卓版

涂色我贼强安卓版

休闲益智 下载
脑细胞太牛了手机版

脑细胞太牛了手机版

休闲益智 下载
球球的冒险旅途

球球的冒险旅途

休闲益智 下载