DBHelper类的关闭问题
来源:互联网 发布:阿里云服务器国际版 编辑:程序博客网 时间:2024/05/22 00:46
DBHelper.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
namespace BookShopSQLDAL
{
static class DBHelper
{
static readonly string _connectionString;
static DBHelper()
{
_connectionString =
ConfigurationManager.ConnectionStrings["conn"].
ConnectionString;
}
public static int ExecuteCommand(string sql, int type)//type = 1表示命令为存储过程
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
if(type==1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
return result;
}
public static int ExecuteCommand(string sql, int type,params SqlParameter[] paras)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(paras);
if (type == 1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
return result;
}
public static SqlDataReader ExecuteReader(string sql, int type,ref SqlConnection conn)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
if (type == 1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader result = cmd.ExecuteReader();
conn = con;
return result;
}
public static SqlDataReader ExecuteReader(string sql, int type, ref SqlConnection conn, params SqlParameter[] paras)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(paras);
if (type == 1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
SqlDataReader result = cmd.ExecuteReader();
conn = con;
return result;
}
public static object ExecuteScalar(string sql, int type)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
if (type == 1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
return result;
}
public static object ExecuteScalar(string sql, int type, params SqlParameter[] paras)
{
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(paras);
if (type == 1)
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
return result;
}
}
}
在返回SqlDataReader 时,数据库是没有关闭的,
但其他页面调用DBHelper的ExecuteReader的这个方法是,没法关闭connection
所以在ExecuteReader方法中传了一个sqlconnection过去,在调用方法是,在接受这个
connection,就能关闭connection了。
不关闭连接,会报连接池的错误
- DBHelper类的关闭问题
- DBHelper类的编写
- 简单的DBHelper类
- WinForms中可能用到辅助类,DBHelper-数据库的打开和关闭
- ASP.NET 的DBHelper类
- 最实用的DBHelper类
- DBHelper类的使用操作
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- DBHelper类
- DBHelper类
- DBHelper类
- DBHelper类
- DBHelper类
- DbHelper 类
- DBHelper类
- CComPtr用法
- 饭桶牌农历控件包3.0 Beta版(带源码)发布啦
- 如何发布VC2005/VC2008编译的程序
- 求救!!!!linux高手请帮忙!!!
- 继承System.Web.UI.Page的页面基类
- DBHelper类的关闭问题
- 汇编语言指令集
- JavaScript实例教程2
- C++的内存分配
- 格式化输出函数: Format
- JMS简明学习教程
- Making a CAB file which doesn’t prompt for installation location
- 暑假数据库知识小结
- ASP.NET控件开发