文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
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();
}
"
相关阅读 更多 +
排行榜 更多 +
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载
战斗火力射击安卓版

战斗火力射击安卓版

飞行射击 下载
空中防御战安卓版

空中防御战安卓版

飞行射击 下载