在现在项目中,数据库操作类是必不可少.网络上也有很多开源的,我想大家比较熟悉的应该是sqlhelper,自己现在用的这份类也是参照网上的资料和以前同事代码中改了些,代码如下:
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Web;
using EMEFRAME.CallDataBase;
namespace EMEFRAME.CallDataBase
{
/**//// <summary>
/// EMESqlHelper 的摘要说明。
/// </summary>
public class EMESqlHelper: System.ComponentModel.Component
{
SqlDataBase sql = new SqlDataBase();
/**//// <summary>
/// Sql 连接字符串,一般情况下在APPLICATION START 中 初始化 联机串
/// </summary>
public static string CnConnectString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
构造函数 初始化连接字符串#region 构造函数 初始化连接字符串
public EMESqlHelper()
{
sql.StrConnect = CnConnectString;
}
#endregion
打开返回的事务#region 打开返回的事务
public SqlTransaction OpenReturnTran()
{
return (SqlTransaction)sql.OpenAndReturnCn().BeginTransaction();
}
#endregion
参数定义和转换#region 参数定义和转换
/**//// <summary>
/// 将字符串转为参数方向
/// </summary>
/// <param name="strDirection"></param>
/// <returns></returns>
private ParameterDirection ConvertStringToParameterDirection(string strDirection)
{
switch (strDirection.ToLower())
{
case "input":
return ParameterDirection.Input;
case "output":
return ParameterDirection.Output;
case "inputoutput":
return ParameterDirection.InputOutput;
case "returnvalue":
return ParameterDirection.ReturnValue;
default:
throw new Exception("数据房间获取错误");
}
}
/**//// <summary>
/// 将字符串转为SQL数据类型
/// </summary>
/// <param name="strParameterType"></param>
/// <returns></returns>
private SqlDbType ConvertStringToDataType(string strParameterType)
{
switch (strParameterType.ToLower())
{
case "bigint":
return SqlDbType.BigInt;
case "binary":
return SqlDbType.Binary;
case "bit":
return SqlDbType.Bit;
case "char":
return SqlDbType.Char;
case "datetime":
return SqlDbType.DateTime;
case "numeric":
return SqlDbType.Float;
case "float":
return SqlDbType.Float;
case "image":
return SqlDbType.Image;
case "int":
return SqlDbType.Int;
case "money":
return SqlDbType.Money;
case "nchar":
return SqlDbType.NChar;
case "ntext":
return SqlDbType.NText;
case "nvarchar":
return SqlDbType.NVarChar;
case "real":
return SqlDbType.Real;
case "smalldatetime":
return SqlDbType.SmallDateTime;
case "smallint":
return SqlDbType.SmallInt;
case "smallmoney":
return SqlDbType.SmallMoney;
case "text":
return SqlDbType.Text;
case "timestamp":
return SqlDbType.Timestamp;
case "tinyint":
return SqlDbType.TinyInt;
case "uniqueIdentifier":
return SqlDbType.UniqueIdentifier;
case "varbinary":
return SqlDbType.VarBinary;
case "varchar":
return SqlDbType.VarChar;
case "variant":
return SqlDbType.Variant;
case "decimal":
return SqlDbType.Decimal;
default:
throw new Exception("数据类型获取失败");
}
}
/**//// <summary>
/// 将字符串转换为类型 枚举
/// </summary>
/// <returns></returns>
private CommandType ConvertStringToCommandType(string strCmdType)
{
switch (strCmdType.ToLower())
{
case "storedprocedure":
return CommandType.StoredProcedure;
case "text":
return CommandType.Text;
case "tabledirect":
return CommandType.TableDirect;
default:
throw new Exception("执行参数获取失败");
}
}
#endregion
无XML版本代码#region 无XML版本代码
执行存储过程或者sql语句命令#region 执行存储过程或者sql语句命令
/**//// <summary>
/// 执行存储过程,需要返回参数时用 ,并且作为事务的一个部分
/// </summary>
/// <param name="sqlTran">事务过程</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">传入参数集合,无时为NULL</param>
/// <param name="array">返回的参数集合,只需要传递一个ARRAYLIST对象,执行成功可返回所有的参数集合</param>
public void ExecCommand(object sqlTran, string cmdText, string cmdType, object[] sqlpar, out ArrayList array)
{
try
{
if (cmdType == "0")
{
sql.CmdType = CommandType.Text;
}
else
{
sql.CmdType = CommandType.StoredProcedure;
}
sql.StrCommand = cmdText;
if (sqlpar != null)
sql.SqlPar = (SqlParameter[])sqlpar;
if (sqlTran != null)
sql.SalTrans = (SqlTransaction)sqlTran;
sql.CmdExecNonQuery();
array = new ArrayList();
if (sqlpar != null)
{
foreach (SqlParameter tmpsqlpar in sqlpar)
{
array.Add(tmpsqlpar.Value);
}
}
}
catch (Exception ex)
{
throw ex;
// throw new Exception("ExecuteNonQuery出错!");
}
}
/**//// <summary>
/// 执行Sql语句 或者 存储过程 并且作为事务过程提交
/// </summary>
/// <param name="sqlTran">事务过程</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">传入参数集合,无时为NULL</param>
public void ExecCommand(object sqlTran, string cmdText, string cmdType, object[] sqlpar)
{
ArrayList array;
ExecCommand(sqlTran, cmdText, cmdType, sqlpar, out array);
}
/**//// <summary>
/// 执行Sql语句 或者 存储过程 并且作为事务过程提交
/// </summary>
/// <param name="sqlTran">事务过程</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">传入参数集合,无时为NULL</param>
public void ExecCommand(object sqlTran, string cmdText, string cmdType)
{
ArrayList array;
ExecCommand(sqlTran, cmdText, cmdType, null, out array);
}
/**//// <summary>
/// 执行存储过程或SQL语句命令
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">传入参数集合,无时为NULL</param>
public void ExecCommand(string cmdText, string cmdType, object[] sqlpar)
{
ArrayList array;
ExecCommand(null, cmdText, cmdType, sqlpar, out array);
}
/**//// <summary>
/// 执行CMDSQL语句或者存储过程,需要返回参数时用
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">传入参数集合,无时为NULL</param>
/// <param name="array">返回的参数集合,只需要传递一个ARRAYLIST对象,执行成功可返回所有的参数集合</param>
public void ExecCommand(string cmdText, string cmdType, object[] sqlpar, out ArrayList array)
{
ExecCommand(null, cmdText, cmdType, sqlpar, out array);
}
/**//// <summary>
/// 执行CMDSQL语句或者存储过程
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
public void ExecCommand(string cmdText, string cmdType)
{
ArrayList array;
ExecCommand(null, cmdText, cmdType, null, out array);
}
#endregion
执行带输出参数存储过程或者sql返回参数集合#region 执行带输出参数存储过程或者sql返回参数集合
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且将参数集合返回
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回参数集合ArrayList</returns>
public ArrayList GetReturnValue(string cmdText, string cmdType, object[] sqlpar)
{
ArrayList array;
ExecCommand(null, cmdText, cmdType, sqlpar, out array);
return array;
}
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且将参数集合返回
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回参数集合ArrayList</returns>
public ArrayList GetReturnValue(string cmdText, string cmdType)
{
ArrayList array;
ExecCommand(null, cmdText, cmdType, null, out array);
return array;
}
#endregion
执行存储过程或者sql返回 Dataset#region 执行存储过程或者sql返回 Dataset
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且将参数集合返回
/// </summary>
/// <param name="sqlTran">事务过程</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回Dataset数据集</returns>
public DataSet GetDataSet(object sqlTran, string cmdText, string cmdType, object[] sqlpar, out ArrayList array)
{
try
{
if (cmdType == "0")
{
sql.CmdType = CommandType.Text;
}
else
{
sql.CmdType = CommandType.StoredProcedure;
}
sql.StrCommand = cmdText;
if (sqlpar != null)
{
sql.SqlPar = (SqlParameter[])sqlpar;
array = new ArrayList();
foreach (SqlParameter tmpsqlpar in sqlpar)
{
array.Add(tmpsqlpar.Value);
}
}
else
{
array = new ArrayList();
}
if (sqlTran != null)
{
sql.SalTrans = (SqlTransaction)sqlTran;
}
return sql.GetDataSet();
}
catch (Exception ex)
{
//throw new Exception("GetDataSet出错!");
throw ex;
}
}
/**//// <summary>
/// 执行存储过程或SQL语句命令
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回Dataset数据集</returns>
public DataSet GetDataSet(object sqlTran, string cmdText, string cmdType, object[] sqlpar)
{
ArrayList array;
return GetDataSet(sqlTran, cmdText, cmdType, sqlpar, out array);
}
/**//// <summary>
/// 执行存储过程或SQL语句命令
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回Dataset数据集</returns>
public DataSet GetDataSet(string cmdText, string cmdType, object[] sqlpar)
{
ArrayList array;
return GetDataSet(null, cmdText, cmdType, sqlpar, out array);
}
/**//// <summary>
/// 执行存储过程或SQL语句命令
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回Dataset数据集</returns>
public DataSet GetDataSet(string cmdText, string cmdType)
{
ArrayList array;
return GetDataSet(null, cmdText, cmdType, null, out array);
}
#endregion
执行方式 返回 Reader#region 执行方式 返回 Reader
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且返回Reader对象
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回Reader对象</returns>
public IDataReader GetReader(string cmdText, string cmdType, object[] sqlpar)
{
try
{
if (cmdType == "0")
{
sql.CmdType = CommandType.Text;
}
else
{
sql.CmdType = CommandType.StoredProcedure;
}
sql.StrCommand = cmdText;
if (sqlpar != null)
{
sql.SqlPar = (SqlParameter[])sqlpar;
}
return sql.GetReader();
}
catch
{
throw new Exception("GetReader出错!");
}
}
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且返回Reader对象
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <returns>返回Reader对象</returns>
public IDataReader GetReader(string cmdText, string cmdType)
{
return GetReader(cmdText, cmdType, null);
}
#endregion
执行命令 并且返回 首条记录的第一个字段#region 执行命令 并且返回 首条记录的第一个字段
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且返回对应的有效第一个字段数据
/// </summary>
/// <param name="sqlTran">事务过程</param>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回第一条记录的第一列字段</returns>
public object ExecCommandScalar(object sqlTran, string cmdText, string cmdType, object[] sqlpar)
{
try
{
if (cmdType == "0")
{
sql.CmdType = CommandType.Text;
}
else
{
sql.CmdType = CommandType.StoredProcedure;
}
if (sqlTran != null)
sql.SalTrans = (SqlTransaction)sqlTran;
if (sqlpar != null)
{
sql.SqlPar = (SqlParameter[])sqlpar;
}
sql.StrCommand = cmdText;
return sql.CmdExecScalar();
}
catch
{
throw new Exception("ExecCommandScalar出错!");
}
}
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且返回对应的有效第一个字段数据
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <param name="sqlpar">参数集合</param>
/// <returns>返回第一条记录的第一列字段</returns>
public object ExecCommandScalar(string cmdText, string cmdType, SqlParameter[] sqlpar)
{
return ExecCommandScalar(null, cmdText, cmdType, sqlpar);
}
/**//// <summary>
/// 执行存储过程 或者 SQL 语句命令 ,并且返回对应的有效第一个字段数据
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">语句类型:0---SQL语句;1---存储过程</param>
/// <returns>返回第一条记录的第一列字段</returns>
public object ExecCommandScalar(string cmdText, string cmdType)
{
return ExecCommandScalar(null, cmdText, cmdType, null);
}
#endregion
获得数据库表sId值最大值#region 获得数据库表sId值最大值
/**//// <summary>
/// 获得数据库表key值最大值
/// </summary>
/// <param name="key">key字段名称</param>
/// <param name="sTable">表名或视图</param>
/// <returns>返回string型(表中最大key值)</returns>
public string GetMax(string key, string sTable)
{
return GetMax(key, sTable, "");
}
/**//// <summary>
/// 获得数据库表key值最大值
/// </summary>
/// <param name="key字">key字段名称</param>
/// <param name="sTable">表名或视图</param>
/// <param name="sWhere">条件语句</param>
/// <returns>返回string型(表中最大key值)</returns>
public string GetMax(string key, string sTable, string sWhere)
{
try
{
sql.CmdType = CommandType.Text;
sql.StrCommand = "select Max(" + key + ") from " + sTable + " where 1=1 " + sWhere;
return sql.CmdExecScalar().ToString();
}
catch
{
throw new Exception("GetMax出错!");
}
}
#endregion
获得记录总数#region 获得记录总数
/**//// <summary>
/// 获得表或视图满足条件的记录总数
/// </summary>
/// <param name="sTable">表名或视图</param>
/// <param name="sWhere">条件语句</param>
/// <returns>返回int型记录总数</returns>
public int GetRecordCount(string sTable, string sWhere)
{
try
{
sql.CmdType = CommandType.Text;
sql.StrCommand = "select count(*) from " + sTable + " where 1=1 " + sWhere;
return (int)sql.CmdExecScalar();
}
catch (Exception ex)
{
throw ex;
//throw new Exception("GetRecordCount出错!");
}
}
/**//// <summary>
/// 获得表或视图全部记录总数
/// </summary>
/// <param name="sTable">表或视图</param>
/// <returns>返回int型记录总数</returns>
public int GetRecordCount(string sTable)
{
return GetRecordCount(sTable, "");
}
#endregion
#endregion
protected override void Dispose(bool disposing)
{
sql.Disponse();
base.Dispose(disposing);
}
}
}
调用例子:
1> 得到记录数
/// <summary>
/// 得到记录数
/// </summary>
/// <returns>返回int</returns>
public static int getWB_SYS_UserCount(string sWhere)
{
int iCount = 0;
try
{
using EMESqlHelper es = new EMESqlHelper())
{
iCount = es.GetRecordCount("WB_SYS_User", sWhere);
}
return iCount;
}
catch (Exception ex)
{
throw ex;
}
}
2> 带返回参数的存储过程调用(注意其中的InputOutput类型)
/// <summary>
/// 新增数据
/// </summary>
/// <param name="_obj">WB_SYS_UserEnt实体</param>
/// <returns>返回bool值</returns>
public static bool WB_SYS_UserAdd(WB_SYS_UserEnt _obj)
{
//*******************定义并赋值参数数组*******************//
SqlParameter[] arParms = new SqlParameter[9];
arParms[0] = new SqlParameter("AdminGroupID", SqlDbType.Int, 4);
arParms[0].Direction = ParameterDirection.Input;
arParms[0].Value = _obj.AdminGroupID;
arParms[1] = new SqlParameter("Username", SqlDbType.NChar, 40);
arParms[1].Direction = ParameterDirection.Input;
arParms[1].Value = _obj.Username;
arParms[2] = new SqlParameter("Password", SqlDbType.NChar, 64);
arParms[2].Direction = ParameterDirection.Input;
arParms[2].Value = _obj.Password;
arParms[3] = new SqlParameter("RealName", SqlDbType.VarChar, 20);
arParms[3].Direction = ParameterDirection.Input;
arParms[3].Value = _obj.RealName;
arParms[4] = new SqlParameter("MobileNum", SqlDbType.VarChar, 20);
arParms[4].Direction = ParameterDirection.Input;
arParms[4].Value = _obj.MobileNum;
arParms[5] = new SqlParameter("PasswordAsk", SqlDbType.VarChar, 200);
arParms[5].Direction = ParameterDirection.Input;
arParms[5].Value = _obj.PasswordAsk;
arParms[6] = new SqlParameter("PasswordAnswer", SqlDbType.VarChar, 200);
arParms[6].Direction = ParameterDirection.Input;
arParms[6].Value = _obj.PasswordAnswer;
arParms[7] = new SqlParameter("Creater", SqlDbType.VarChar, 20);
arParms[7].Direction = ParameterDirection.Input;
arParms[7].Value = _obj.Creater;
bool Exist = false;
arParms[8] = new SqlParameter("Exist", SqlDbType.Bit, 1);
arParms[8].Direction = ParameterDirection.InputOutput;
arParms[8].Value = Exist;
try
{
using EMESqlHelper es = new EMESqlHelper())
{
es.ExecCommand("WB_SYS_User_Add", "1", arParms);
Exist = (bool)arParms[8].Value;
}
return !Exist;
}
catch (Exception ex)
{
throw ex;
}
}
其中关联类SqlDataBase和DataBase的代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
namespace EMEFRAME.CallDataBase
{
/**//// <summary>
/// SqlDataBase 的摘要说明。
/// </summary>
public class SqlDataBase : DataBase
{
数据属性#region 数据属性
private string strConnect ;
public string StrConnect
{
set {strConnect = value;}
}
private string strCommand;
public string StrCommand
{
set {strCommand =value;}
}
private CommandType cmdType;
public CommandType CmdType
{
set {cmdType =value ;}
}
private SqlParameter [] sqlpar ;
public SqlParameter [] SqlPar
{
set {sqlpar = value;}
}
private SqlTransaction sqlTrans;
public SqlTransaction SalTrans
{
set {sqlTrans = value;}
}
SqlConnection cn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader reader = null;
SqlDataAdapter adp = new SqlDataAdapter();
DataSet ds =new DataSet();
#endregion
public SqlDataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
打开数据库#region 打开数据库
/**//// <summary>
/// 打开数据库
/// </summary>
/// <returns></returns>
public override bool OpenCn()
{
if (cn.State != ConnectionState.Open )
{
try
{
cn.ConnectionString = strConnect;
cn.Open ();
return true;
}
catch
{
return false;
}
}
else
return true;
}
/**//// <summary>
/// 打开并且返回SQLCONNECTION
/// </summary>
/// <returns></returns>
public override IDbConnection OpenAndReturnCn()
{
if (cn.State != ConnectionState.Open )
{
try
{
OpenCn();
return cn;
}
catch
{
return null;
}
}
else
return cn;
}
#endregion
执行存储过程#region 执行存储过程
/**//// <summary>
/// 执行存储过程
/// </summary>
/// <returns>返回存储过程返回值</returns>
public void RunProc()
{
if (sqlTrans == null)
{
if (OpenCn())
cmd.Connection =cn;
else
return ;
}
else
{
cmd.Transaction = sqlTrans;
cmd.Connection = sqlTrans.Connection;
}
cmd.CommandText = strCommand;
cmd.CommandType = cmdType;
if (sqlpar!=null)
{
foreach(SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
try
{
cmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
throw e;
}
}
#endregion
执行 CMD#region 执行 CMD
/**//// <summary>
/// 执行cmd,不返回数据
/// </summary>
public override void CmdExecNonQuery()
{
if (sqlTrans == null)
{
if (OpenCn())
cmd.Connection =cn;
else
return ;
}
else
{
cmd.Transaction = sqlTrans;
cmd.Connection = sqlTrans.Connection;
}
cmd.CommandText = strCommand;
cmd.CommandType = cmdType;
if (sqlpar!=null)
{
foreach(SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
cmd.ExecuteNonQuery ();
}
public override object CmdExecScalar()
{
if (sqlTrans == null)
{
if (OpenCn())
cmd.Connection =cn;
else
return null;
}
else
{
cmd.Transaction = sqlTrans;
cmd.Connection = sqlTrans.Connection;
}
cmd.CommandText = strCommand;
cmd.CommandType = cmdType;
if (sqlpar!=null)
{
foreach(SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
return cmd.ExecuteScalar();
}
#endregion
返回DATASET#region 返回DATASET
/**//// <summary>
/// 返回DataSet
/// </summary>
/// <returns></returns>
public override DataSet GetDataSet()
{
if (sqlTrans == null)
{
if (OpenCn())
cmd.Connection =cn;
else
return null;
}
else
{
cmd.Transaction = sqlTrans;
cmd.Connection = sqlTrans.Connection;
}
cmd.CommandText = strCommand;
cmd.CommandType = cmdType;
if (sqlpar!=null)
{
foreach (SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
adp.SelectCommand = cmd ;
ds.Clear ();
adp.Fill (ds);
return ds;
}
public override DataSet GetDataSet(string TableName)
{
if (OpenCn())
cmd.Connection =cn;
else
return null;
cmd.CommandText = strCommand;
cmd.CommandType = cmdType;
if (sqlpar!=null)
{
foreach (SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
adp.SelectCommand = cmd ;
ds.Clear();
adp.Fill (ds,TableName);
return ds;
}
#endregion
返回READER#region 返回READER
public override IDataReader GetReader()
{
if (OpenCn())
cmd.Connection = cn;
else
return null;
cmd.CommandText = strCommand ;
cmd.CommandType =cmdType;
if (sqlpar!=null)
{
foreach (SqlParameter Nsqlpar in sqlpar)
{
cmd.Parameters.Add (Nsqlpar);
}
}
try
{
reader = cmd.ExecuteReader ();
return reader;
}
catch
{
return null;
}
}
#endregion
卸载内存#region 卸载内存
public override void ClearParameter()
{
cmd.Parameters.Clear();
}
public override void CloseReader()
{
if (reader != null)
if (!reader.IsClosed)
reader.Close ();
}
public override void CloseCn()
{
if (cn != null && cn.State !=ConnectionState.Closed )
cn.Close ();
}
public override void Disponse()
{
cmd.Dispose ();
CloseReader();
adp.Dispose ();
ds.Dispose ();
CloseCn();
cn.Dispose ();
}
#endregion
}
}
using System;
using System.Data;
namespace EMEFRAME.CallDataBase
{
/**//// <summary>
/// DataBase 的摘要说明。
/// </summary>
public abstract class DataBase
{
public DataBase()
{
}
public abstract bool OpenCn ();
public abstract IDbConnection OpenAndReturnCn();
public abstract IDataReader GetReader ();
public abstract DataSet GetDataSet (string TableName);
public abstract DataSet GetDataSet();
public abstract void CmdExecNonQuery ();
public abstract object CmdExecScalar ();
public abstract void CloseCn ();
public abstract void CloseReader ();
public abstract void ClearParameter ();
public abstract void Disponse ();
}
}