>>
51aspx首页
| >>
Winform源码
| >>
.Net源码大搜捕
| >>
问题/帮助
登录
注册
标签
会员
搜索
帮助
Asp.net源码交流论坛
»
Asp.net专题讨论区
»
ADO.net讨论区
»
自己写的SQL数据库基类(转)
帖子标题
业务申请区
实名认证申请
免费.Net主机申请
积分奖励申请
有偿服务申请
Asp.net交流讨论区
Asp.net技术问答
.Net项目、服务交易区
.Net源码问答区
Asp.net技术文章
常用工具下载
Asp.net资源发布区
[源码发布区]
[视频发布区]
[商业代码区]
[其他.net相关资源]
Asp.net专题讨论区
Ajax/Atlas无刷新技术
面向对象开发
ADO.net讨论区
控件专题讨论
源码环境搭建和部署视频!
源码使用常见问题
51Aspx自有服务产品 开业特惠
快速通道:
[
源码发布
]
|
[
实名认证
]
1/2页
1
2
跳转到
页
查看:11428
键盘左右键可以进行前后翻页操作
自己写的SQL数据库基类(转)
51aspx
51aspx
组别
[
超级管理员
]
性别
积分
[
2800
]
帖子
2071
注册时间
2007-05-17
51aspx
2007-06-28 08:32
| 只看楼主
树型
|
收藏
|
小
中
大
1
#
自己写的SQL数据库基类(转)
[
实名贴,值得关注]
[复制到剪贴板]
CODE:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// MySqlHelper 的摘要说明
/// </summary>
public class MySqlHelper
{
//private SqlConnection Conn;
private SqlCommand Comm;
private SqlDataAdapter da;
private DataSet ds;
private DataTable dt;
private SqlDataReader dr;
private SqlTransaction trans;
public static string ConnString = System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
public static SqlConnection Conn()
{
SqlConnection Conn = new SqlConnection(ConnString);
return Conn;
}
//
// TODO: 在此处添加构造函数逻辑
//
/// <summary>无参数命令或存储过程构造MySqlCommand对象</summary>
/// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
/// <param name="commandType">类型(Sql命令或存储过程)</param>
///
private SqlCommand buildSqlCommand(string commandTextOrProcedureName, CommandType commandType)
{
try
{
Comm = new SqlCommand(commandTextOrProcedureName, Conn());
Comm.CommandType = commandType;
return Comm;
}
catch (Exception error)
{
throw error;
}
}
/// <summary>在新的MySqlConnection中以无参数命令或存储过程构造MySqlCommand对象</summary>
/// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
/// <param name="commandType">类型(Sql命令或存储过程)</param>
/// <param name="newConn">新的MySqlConnection对象</param>
private SqlCommand buildSqlCommand(string commandTextOrProcedureName, CommandType commandType, SqlConnection newConn)
{
try
{
Comm = new SqlCommand(commandTextOrProcedureName, newConn);
Comm.CommandType =commandType;
return Comm;
}
catch (Exception error)
{
throw error;
}
}
///// <summary>有参数命令或存储过程构造MySqlCommand对象</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">SqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
private SqlCommand buildSqlCommand(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType)
{
try
{
Comm = new SqlCommand(commandTextOrProcedureName,Conn());
Comm.CommandType = commandType;
foreach (SqlParameter parameter in parameters)
{
Comm.Parameters.Add(parameter);
}
return Comm;
}
catch (Exception error)
{
throw error;
}
}
///// <summary>在新的MySqlConnection中以有参数命令或存储过程构造MySqlCommand对象</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">MySqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="newConn">新的MySqlConnection对象</param>
private SqlCommand buildSqlCommand(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType, SqlConnection newConn)
{
try
{
Comm = new SqlCommand(commandTextOrProcedureName, newConn);
Comm.CommandType = commandType;
foreach (SqlParameter parameter in parameters)
{
Comm.Parameters.Add(parameter);
}
return Comm;
}
catch (Exception error)
{
throw error;
}
}
///**/
///// <summary>执行无参数查询命令或存储过程,返回第一行第一列的值</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
///// <returns>返回第一行第一列的值,如果结果为null则返回空字符串</returns>
private string ExecuteScalar(string commandTextOrProcedureName, CommandType commandType, bool isNewConnection)
{
string result = "";
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
Comm = new SqlCommand(commandTextOrProcedureName, newConn);
newConn.Open();
if (Comm.ExecuteScalar() != null)
result = Comm.ExecuteScalar().ToString();
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType);
if (Comm.ExecuteScalar() != null)
result = Comm.ExecuteScalar().ToString();
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
return result;
}
return result;
}
///**/
///// <summary>执行有参数查询命令或存储过程,返回第一行第一列的值</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">SqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
///// <returns>返回第一行第一列的值,如果结果为null则返回空字符串</returns>
protected string ExecuteScalar(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType, bool isNewConnection)
{
string result = "";
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType, newConn);
newConn.Open();
if (Comm.ExecuteScalar() != null)
result = Comm.ExecuteScalar().ToString();
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType);
if (Comm.ExecuteScalar() != null)
result = Comm.ExecuteScalar().ToString();
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
return result;
}
// /// <summary>执行无参数命令或存储过程,返回受影响行数</summary>
// /// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
// /// <param name="commandType">类型(Sql命令或存储过程)</param>
// /// <param name="isNewConnection">是否使用新的连接执行</param>
// /// <returns>返回受影响的行数</returns>
protected int ExecuteNonQuery(string commandTextOrProcedureName, CommandType commandType, bool isNewConnection)
{
int result = 0;
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType, newConn);
newConn.Open();
result = Comm.ExecuteNonQuery();
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType);
result = Comm.ExecuteNonQuery();
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
return result;
}
///**/
///// <summary>执行有参数命令或存储过程,返回受影响行数</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">SqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
///// <returns>返回受影响的行数</returns>
protected int ExecuteNonQuery(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType, bool isNewConnection)
{
int result = 0;
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType, newConn);
newConn.Open();
result = Comm.ExecuteNonQuery();
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType);
result = Comm.ExecuteNonQuery();
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
return result;
}
///**/
///// <summary>执行无参数查询命令或存储过程,返回SqlDataReader对象(注意用完后及时关闭MySqlDataReader对象)</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
///// <returns>MySqlDataReader(当MySqlDataReader对象关闭时对应的MySqlConnection对象将关闭)</returns>
protected SqlDataReader ExecuteReader(string commandTextOrProcedureName, CommandType commandType, bool isNewConnection)
{
if (isNewConnection)
{
try
{
SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString);
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType, newConn);
newConn.Open();
return Comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception error)
{
throw error;
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType);
return Comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception error)
{
throw error;
}
}
}
///// <summary>执行有参数查询命令或存储过程,返回SqlDataReader对象(注意用完后及时关闭MySqlDataReader对象)</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">MySqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
///// <returns>MySqlDataReader(当MySqlDataReader对象关闭时对应的MySqlConnection对象将关闭)</returns>
protected SqlDataReader ExecuteReader(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType, bool isNewConnection)
{
if (isNewConnection)
{
try
{
SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString);
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType, newConn);
newConn.Open();
return Comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception error)
{
throw error;
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType);
return Comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception error)
{
throw error;
}
}
}
///// <summary>执行无参数查询命令或存储过程,返回DataSet</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否用新的连接执行</param>
///// <returns></returns>
public DataSet ExecuteDataSet(string commandTextOrProcedureName, CommandType commandType, bool isNewConnection)
{
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
newConn.Open();
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType, newConn);
da = new SqlDataAdapter(Comm);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, commandType);
da = new SqlDataAdapter(Comm);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
}
///// <summary>执行有参数查询命令或存储过程,返回DataSet</summary>
///// <param name="commandTextOrProcedureName">Sql命令或存储过程名</param>
///// <param name="parameters">SqlParameter参数数组</param>
///// <param name="commandType">类型(Sql命令或存储过程)</param>
///// <param name="isNewConnection">是否用新的连接执行</param>
///// <returns></returns>
protected DataSet ExecuteDataSet(string commandTextOrProcedureName, SqlParameter[] parameters, CommandType commandType, bool isNewConnection)
{
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
newConn.Open();
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType, newConn);
da = new SqlDataAdapter(Comm);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception error)
{
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(commandTextOrProcedureName, parameters, commandType);
da = new SqlDataAdapter(Comm);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
}
///**/
///// <summary>
///// 事务处理
///// </summary>
///// <param name="cmdArray">MySqlCommand数组</param>
///// <param name="isNewConnection">是否使用新的连接执行</param>
protected void ExecuteTransaction(SqlCommand[] cmdArray, bool isNewConnection)
{
if (isNewConnection)
{
using (SqlConnection newConn = new SqlConnection(MySqlHelper.ConnString))
{
try
{
newConn.Open();
trans = newConn.BeginTransaction();
foreach (SqlCommand c in cmdArray)
{
c.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception error)
{
trans.Rollback();
throw error;
}
finally
{
newConn.Close();
}
}
}
else
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
trans = Conn().BeginTransaction();
foreach (SqlCommand c in cmdArray)
{
c.ExecuteNonQuery();
}
trans.Commit();
}
catch (Exception error)
{
trans.Rollback();
throw error;
}
finally
{
Conn().Close();
}
}
}
///// <summary>执行有参数且返回值为Int型的存储过程,获取返回结果</summary>
///// <param name="commandTextOrProcedureName">存储过程名</param>
///// <param name="parameters">SqlParameter参数数组</param>
///// <returns>Int</returns>
protected int ExecuteProcedure(string procedureName, SqlParameter[] parameters)
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(procedureName, parameters, CommandType.StoredProcedure);
Comm.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, String.Empty, DataRowVersion.Default, null));
Comm.ExecuteNonQuery();
return (int)Comm.Parameters["ReturnValue"].Value;
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
///// 执行分页存储过程,存储过程的第一个参数必须为out用于输出记录总数
///// </summary>
///// <remarks>parameters中勿使用表别名,SQL语法尽量标准</remarks>
///// <param name="rowsTotal">返回的记录总数</param>
///// <param name="procedureName">分页存储过程名称</param>
///// <param name="parameters">分页存储过程参数</param>
///// <returns></returns>
protected DataSet ExecuteProcedure(out int rowsTotal, string procedureName, SqlParameter[] parameters)
{
try
{
if (Conn().State == ConnectionState.Closed)
{
Conn().Open();
}
Comm = this.buildSqlCommand(procedureName, parameters, CommandType.StoredProcedure);
ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(Comm);
da.Fill(ds);
rowsTotal = int.Parse(parameters[0].Value.ToString());
return ds;
}
catch (Exception error)
{
throw error;
}
finally
{
Conn().Close();
}
}
}
51aspx 最后编辑于 2007-12-02 09:51:24
技术问题请直接发布到论坛,客户服务QQ:1120930903,合作及咨询QQ:793095132
常见问题:
点这里
,请仔细查看!
基础配置视频:
点这里
问题搜索:
请点击
!
积分奖励:
关于论坛实名版块的建议...
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
2
精华
27
威望
594 分
金钱
364.6 元
来自
北京
状态
在线
793095132
simonfan
组别
[
学员
]
性别
积分
[
4
]
帖子
4
注册时间
2007-06-28
simonfan
2007-06-28 12:02
树型
|
收藏
|
小
中
大
2
#
回复:自己写的SQL数据库基类(转)
楼主写的不错 我越看越像petshop里面的
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
317
精华
0
威望
0 分
金钱
0.55 元
来自
状态
离线
scyyzgxh
组别
[
工程师
]
性别
积分
[
136
]
帖子
63
注册时间
2007-06-11
scyyzgxh
2007-07-09 12:07
树型
|
收藏
|
小
中
大
3
#
回复: 自己写的SQL数据库基类(转)
代码似乎引用了配置文件.不知道web.config里面的字符串应该怎么样写?
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
121
精华
0
威望
73 分
金钱
-3.05 元
来自
状态
离线
flyerwon
小迷糊
组别
[
工程师
]
性别
生日
2005-11-26
积分
[
405
]
帖子
357
注册时间
2007-11-30
flyerwon
2007-12-02 01:31
树型
|
收藏
|
小
中
大
4
#
回复:自己写的SQL数据库基类(转)
学习!
不错1
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
1267
精华
0
威望
48 分
金钱
33.4 元
来自
西安
状态
离线
75641535
乱世英雄
组别
[
实习生
]
性别
积分
[
11
]
帖子
11
注册时间
2007-12-25
乱世英雄
2007-12-27 12:44
树型
|
收藏
|
小
中
大
5
#
回复:自己写的SQL数据库基类(转)
不错
学习学习
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
3468
精华
0
威望
0 分
金钱
1.1 元
来自
状态
离线
aspxgwz
组别
[
学员
]
性别
积分
[
1
]
帖子
1
注册时间
2008-01-08
aspxgwz
2008-01-08 10:41
树型
|
收藏
|
小
中
大
6
#
回复:自己写的SQL数据库基类(转)
参考学习...
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
4722
精华
0
威望
0 分
金钱
0.1 元
来自
状态
离线
huangykj
组别
[
实习生
]
性别
积分
[
11
]
帖子
8
注册时间
2007-12-03
huangykj
2008-03-06 09:30
树型
|
收藏
|
小
中
大
7
#
回复:自己写的SQL数据库基类(转)
好像还可以
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
1444
精华
0
威望
3 分
金钱
-2.5 元
来自
状态
离线
cexopfwvfp
组别
[
学员
]
性别
积分
[
5
]
帖子
5
注册时间
2008-07-29
cexopfwvfp
2008-07-30 18:52
树型
|
收藏
|
小
中
大
8
#
回复:自己写的SQL数据库基类(转)
不错的类,很实用!
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
26457
精华
0
威望
0 分
金钱
0.5 元
来自
状态
离线
cexopfwvfp
组别
[
学员
]
性别
积分
[
5
]
帖子
5
注册时间
2008-07-29
cexopfwvfp
2008-07-30 18:52
树型
|
收藏
|
小
中
大
9
#
回复:自己写的SQL数据库基类(转)
不错的类,很实用!
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
26457
精华
0
威望
0 分
金钱
0.5 元
来自
状态
离线
cexopfwvfp
组别
[
学员
]
性别
积分
[
5
]
帖子
5
注册时间
2008-07-29
cexopfwvfp
2008-07-30 18:52
树型
|
收藏
|
小
中
大
10
#
回复:自己写的SQL数据库基类(转)
不错的类,很实用!
TOP
发送短消息
查看公共资料
查找该会员全部帖子
UID
26457
精华
0
威望
0 分
金钱
0.5 元
来自
状态
离线
<<
上一主题
|
下一主题
>>
1/2页
1
2
跳转到
页
发新主题
论坛跳转...
业务申请区
Asp.net交流讨论区
Asp.net技术问答
.Net项目、服务交易区
.Net源码问答区
Asp.net技术文章
[源码发布区]
[视频发布区]
[商业代码区]
[其他.net相关资源]
常用工具下载
Asp.net资源发布区
Asp.net专题讨论区
Ajax/Atlas无刷新技术
面向对象开发
ADO.net讨论区
控件专题讨论
51aspx站务相关
常见问题&帮助
站内活动、公告
反馈/建议
灌水/非技术
[垃圾箱/恶性广告]
实名认证申请
免费.Net主机申请
积分奖励申请
有偿服务申请
我的资料
我的主题
我的回复
我的精华
我的附件
我的收藏
基本状况
流量统计
客户软件
发帖量记录
版块排行
主题排行
发帖排行
积分排行
在线时间
帖子标题
作 者