- 一些应该熟记于心的jQuery函数和技巧
- javascript/jquery获取地址栏url参数的方法
- 什么是淘宝公域流量和私域流量?都有那些?
- ASP的域名重定向到目录功能的实现
- 从零开始学习jQuery (九) jQuery工具函数
- SEO中导致网站降权具体表现及解决办法
- 盲盒交友项目解析
- System.NullReferenceException 未将对象引用设置到对象的实例
- 构建技术方
- 总结地方论坛推广的几个小思路
邮箱:
手机:15383239821
C#中释放数据库连接资源
1.确保释放数据库连接资源的两种方式如下:
a.使用try...catch...finally语句块,在finally块中关闭连接;
b.使用using语句块,无论如何退出,都会自动关闭连接;
2.最好的方法是组合使用以上两种方式。
using System;
using System.Data.SqlClient;
namespace Magci.Test.DataAccess
{
class Program
{
static void Main(string[] args)
{
string source = @"server=.\sqlexpress; integrated security=SSPI; database=msdb";
SqlConnection conn = null;
//使用try块处理
try
{
conn = new SqlConnection(source);
conn.Open();
//Do something
}
catch (Exception e)
{
//Do something with the exception
}
finally
{
conn.Close();
}
//使用using块处理
using (conn = new SqlConnection(source))
{
conn.Open();
//Do something
}
//两种组合方式
try
{
using (conn = new SqlConnection(source))
{
conn.Open();
//Do something
conn.Close();
}
}
catch (Exception e)
{
//Do something with the exception
}
}
}
}
- 上一篇:C#中split用法
- 下一篇:cstr用法
-
2019-03-27近期完成的部分客户网站设计稿
-
2023-09-13Vue 3.0 响应性 基础
-
2018-11-08网站建设
-
2010-08-09一些应该熟记于心的jQuery函数和技巧
-
2010-10-04c#中this的用法
-
2020-08-04创业想做电商运营必须注意的细节问题
