2023年6月21日发(作者:)

using System;

using ;

using ent;

using uration;

using c;

namespace

{

///

/// 数据库操作基类

/// 实现对Sql数据库的各种操作

/// 创建时间:2010-1-18

///

public class DBHelper

{

#region 初始化字段

private string SqlConnectionString = "";

#endregion

#region DBHelper(string Conn_Str): 构造函数,以Conn_Str为数据库连接字符串name

///

/// 确定Conn_Str标识的数据库连接字符串

///

public DBHelper(string Conn_Str)

{

//获取数据库连接字符串

//SqlConnectionString

tionStrings[Conn_Str].connectionString;

SqlConnectionString = tionStrings[Conn_Str].ConnectionString;

}

#endregion

#region ExecSqlCommand: 执行SQL语句,返回bool型结果,是否执行成功

///

/// 执行SQL语句,返回bool

///

/// insert、update、delete

///

public bool ExecSqlCommand(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

= using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

int i = eNonQuery();

return i > 0 ? true : false;

}

catch (SqlException e)

{

();

return false;

throw e;

}

finally

{

();

}

}

}

}

#endregion

#region ExecuteNonQuery: 执行SQL语句,返回影响的记录数

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public int ExecuteNonQuery(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

int rows = eNonQuery();

return rows;

}

catch (SqlException e)

{ ();

throw e;

}

}

}

}

public int ExecuteNonQuery(string cmdText, CommandType ct)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(cmdText, con))

{

try

{

dType = ct;

();

int rows = eNonQuery();

return rows;

}

catch (SqlException e)

{

();

throw e;

}

}

}

}

#endregion

#region ExecuteBoolQuery: 执行带参数的SQL语句(带参)返回bool

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public bool ExecuteBoolQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString)) {

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

int rows = eNonQuery();

();

return rows > 0 ? true : false;

}

catch (SqlException e)

{

return false;

throw e;

}

finally

{

();

}

}

}

}

#endregion

#region ExecuteNonQuery: 执行带参数的SQL语句(带参)

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public int ExecuteNonQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

int rows = eNonQuery();

();

return rows;

}

catch (SqlException e) {

throw e;

}

}

}

}

public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(cmdText, con))

{

try

{

dType = ct;

ge(paras);

int rows = eNonQuery();

();

return rows;

}

catch (SqlException e)

{

throw e;

}

}

}

}

#endregion

#region ExecuteTransaction: 执行多条SQL语句,实现数据库事务

///

/// 执行多条SQL语句,实现数据库事务。

///

/// 多条SQL语句

public int ExecuteTransaction(List sqlList)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

();

SqlCommand cmd = new SqlCommand();

tion = con;

SqlTransaction tx = ransaction();

ction = tx;

try

{

int count = 0;

for (int i = 0; i < ; i++)

{

string sql = sqlList[i];

if (().Length > 1)

{

dText = sql;

count += eNonQuery();

}

}

();

return count;

}

catch

{

ck();

return 0;

}

}

}

#endregion

#region ExecuteReader: 执行查询语句,返回SqlDataReader

///

/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )

///

/// 查询语句

/// SqlDataReader

public SqlDataReader ExecuteReader(string sql)

{

SqlConnection con = new SqlConnection(SqlConnectionString);

SqlCommand cmd = new SqlCommand(sql, con);

try

{

();

SqlDataReader reader = eReader(onnection);

return reader;

}

catch (SqlException e)

{

throw e; }

}

#endregion

#region ExecuteReader: 执行查询语句,返回SqlDataReader(带参)

///

/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )

///

/// 查询语句

/// SqlDataReader

public SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters)

{

SqlConnection con = new SqlConnection(SqlConnectionString);

SqlCommand cmd = new SqlCommand();

try

{

PrepareCommand(cmd, con, null, sql, parameters);

SqlDataReader reader = eReader(onnection);

();

return reader;

}

catch (SqlException e)

{

throw e;

}

}

#endregion

#region ExecuteQuery: 执行查询语句,返回DataTable

///

/// 执行查询语句,返回DataTable

///

/// 查询语句

/// DataTable

public DataTable ExecuteQuery(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

DataTable table = new DataTable();

try

{

();

SqlDataAdapter da = new SqlDataAdapter(sql, con); (table);

}

catch (SqlException ex)

{

throw new Exception(e);

}

return table;

}

}

#endregion

#region ExecuteQuery: 执行查询语句,返回DataTable(带参)

///

/// 执行查询语句,返回DataTable

///

/// 查询语句

/// DataTable

public DataTable ExecuteQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

SqlCommand cmd = new SqlCommand();

PrepareCommand(cmd, con, null, sql, parameters);

using (SqlDataAdapter da = new SqlDataAdapter(cmd))

{

DataTable table = new DataTable();

try

{

(table);

();

}

catch (SqlException ex)

{

throw new Exception(e);

}

return table;

}

}

}

#endregion

#region PrepareCommand: 处理参数

private void PrepareCommand(SqlCommand cmd, SqlConnection con, SqlTransaction tran, string

cmdText, SqlParameter[] parameters) {

if ( != )

();

tion = con;

dText = cmdText;

if (tran != null)

ction = tran;

dType = ;

if (parameters != null)

{

foreach (SqlParameter parameter in parameters)

{

if ((ion == utput || ion ==

) && ( == null))

{

= ;

}

(parameter);

}

}

}

#endregion

#region ExecuteScalar: 执行一条计算查询结果语句,返回查询结果object(带参)

///

/// 执行一条计算查询结果语句,返回查询结果(object)。

///

/// 计算查询结果语句

/// 查询结果(object)

public object ExecuteScalar(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

object obj = eScalar();

();

if (((obj, null)) || ((obj, )))

{

return null;

} else

{

return obj;

}

}

catch (SqlException e)

{

throw e;

}

}

}

}

#endregion

#region ExecuteScalar: 执行一条计算查询结果语句,返回查询结果object

///

/// 执行一条计算查询结果语句,返回查询结果(object)。

///

/// 计算查询结果语句

/// 查询结果(object)

public object ExecuteScalar(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

object obj = eScalar();

if (((obj, null)) || ((obj, )))

{

return null;

}

else

{

return obj;

}

}

catch (SqlException e)

{

();

throw e;

} }

}

}

#endregion

#region GetDs:执行一条计算查询结果语句,返回查询结果DataSet

///

/// 返回DataSet数据集

///

/// SQL语句

public DataSet GetDs(string strSql)

{

DataSet ds;

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlDataAdapter sda = new SqlDataAdapter(strSql, con))

{

try

{

();

ds = new DataSet();

(ds);

}

catch (SqlException e)

{

();

throw e;

}

finally

{

();

}

}

}

return ds;

}

#endregion

#region GetDs:执行一条计算查询结果语句,返回查询结果装载到参数DataSet中,无返回值

///

/// 添加DataSet表

///

/// DataSet对象

/// Sql语句 /// 表名

public void GetDs(DataSet dss, string strSql, string strTableName)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlDataAdapter sda = new SqlDataAdapter(strSql, con))

{

try

{

();

(dss, strTableName);

}

catch (SqlException e)

{

();

throw e;

}

finally

{

();

}

}

}

}

#endregion

/////

///// 执行不带参数的增删改SQL语句或存储过程

/////

///// 增删改SQL语句或存储过程

///// 命令类型

/////

//public int ExecuteNonQuery(string cmdText, CommandType ct)

//{

// int res;

// try

// {

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// res = eNonQuery();

// }

// catch (Exception ex)

// { // throw ex;

// }

// finally

// {

// if ( == )

// {

// ();

// }

// }

// return res;

//}

/////

///// 执行带参数的增删改SQL语句或存储过程

/////

///// 增删改SQL语句或存储过程

///// 命令类型

///// int值

//public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// int res;

// using (cmd = new SqlCommand(cmdText, GetConn()))

// {

// dType = ct;

// ge(paras);

// res = eNonQuery();

// }

// return res;

//}

/////

///// 执行查询SQL语句或存储过程

/////

///// 查询SQL语句或存储过程

///// 命令类型

///// Table值

//public DataTable ExecuteQuery(string cmdText, CommandType ct)

//{

// DataTable dt = new DataTable();

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// using (sdr = eReader(onnection))

// {

// (sdr); // }

// return dt;

//}

/////

///// 执行带参数的查询SQL语句或存储过程

/////

///// 查询SQL语句或存储过程

///// 参数集合

///// 命令类型

///// Table值

//public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// DataTable dt = new DataTable();

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// ge(paras);

// using (sdr = eReader(onnection))

// {

// (sdr);

// }

// return dt;

//}

/////

///// 执行带参数的Scalar查询

/////

///// 查询SQL语句或存储过程

///// 参数集合

///// 命令类型

///// 一个int型值

//public int ExecuteCheck(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// int result;

// using (cmd = new SqlCommand(cmdText, GetConn()))

// {

// dType = ct;

// ge(paras);

// result = 32(eScalar());

// }

// return result;

//}

}

}

2023年6月21日发(作者:)

using System;

using ;

using ent;

using uration;

using c;

namespace

{

///

/// 数据库操作基类

/// 实现对Sql数据库的各种操作

/// 创建时间:2010-1-18

///

public class DBHelper

{

#region 初始化字段

private string SqlConnectionString = "";

#endregion

#region DBHelper(string Conn_Str): 构造函数,以Conn_Str为数据库连接字符串name

///

/// 确定Conn_Str标识的数据库连接字符串

///

public DBHelper(string Conn_Str)

{

//获取数据库连接字符串

//SqlConnectionString

tionStrings[Conn_Str].connectionString;

SqlConnectionString = tionStrings[Conn_Str].ConnectionString;

}

#endregion

#region ExecSqlCommand: 执行SQL语句,返回bool型结果,是否执行成功

///

/// 执行SQL语句,返回bool

///

/// insert、update、delete

///

public bool ExecSqlCommand(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

= using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

int i = eNonQuery();

return i > 0 ? true : false;

}

catch (SqlException e)

{

();

return false;

throw e;

}

finally

{

();

}

}

}

}

#endregion

#region ExecuteNonQuery: 执行SQL语句,返回影响的记录数

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public int ExecuteNonQuery(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

int rows = eNonQuery();

return rows;

}

catch (SqlException e)

{ ();

throw e;

}

}

}

}

public int ExecuteNonQuery(string cmdText, CommandType ct)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(cmdText, con))

{

try

{

dType = ct;

();

int rows = eNonQuery();

return rows;

}

catch (SqlException e)

{

();

throw e;

}

}

}

}

#endregion

#region ExecuteBoolQuery: 执行带参数的SQL语句(带参)返回bool

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public bool ExecuteBoolQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString)) {

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

int rows = eNonQuery();

();

return rows > 0 ? true : false;

}

catch (SqlException e)

{

return false;

throw e;

}

finally

{

();

}

}

}

}

#endregion

#region ExecuteNonQuery: 执行带参数的SQL语句(带参)

///

/// 执行SQL语句,返回影响的记录数

///

/// SQL语句

/// 影响的记录数

public int ExecuteNonQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

int rows = eNonQuery();

();

return rows;

}

catch (SqlException e) {

throw e;

}

}

}

}

public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(cmdText, con))

{

try

{

dType = ct;

ge(paras);

int rows = eNonQuery();

();

return rows;

}

catch (SqlException e)

{

throw e;

}

}

}

}

#endregion

#region ExecuteTransaction: 执行多条SQL语句,实现数据库事务

///

/// 执行多条SQL语句,实现数据库事务。

///

/// 多条SQL语句

public int ExecuteTransaction(List sqlList)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

();

SqlCommand cmd = new SqlCommand();

tion = con;

SqlTransaction tx = ransaction();

ction = tx;

try

{

int count = 0;

for (int i = 0; i < ; i++)

{

string sql = sqlList[i];

if (().Length > 1)

{

dText = sql;

count += eNonQuery();

}

}

();

return count;

}

catch

{

ck();

return 0;

}

}

}

#endregion

#region ExecuteReader: 执行查询语句,返回SqlDataReader

///

/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )

///

/// 查询语句

/// SqlDataReader

public SqlDataReader ExecuteReader(string sql)

{

SqlConnection con = new SqlConnection(SqlConnectionString);

SqlCommand cmd = new SqlCommand(sql, con);

try

{

();

SqlDataReader reader = eReader(onnection);

return reader;

}

catch (SqlException e)

{

throw e; }

}

#endregion

#region ExecuteReader: 执行查询语句,返回SqlDataReader(带参)

///

/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )

///

/// 查询语句

/// SqlDataReader

public SqlDataReader ExecuteReader(string sql, params SqlParameter[] parameters)

{

SqlConnection con = new SqlConnection(SqlConnectionString);

SqlCommand cmd = new SqlCommand();

try

{

PrepareCommand(cmd, con, null, sql, parameters);

SqlDataReader reader = eReader(onnection);

();

return reader;

}

catch (SqlException e)

{

throw e;

}

}

#endregion

#region ExecuteQuery: 执行查询语句,返回DataTable

///

/// 执行查询语句,返回DataTable

///

/// 查询语句

/// DataTable

public DataTable ExecuteQuery(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

DataTable table = new DataTable();

try

{

();

SqlDataAdapter da = new SqlDataAdapter(sql, con); (table);

}

catch (SqlException ex)

{

throw new Exception(e);

}

return table;

}

}

#endregion

#region ExecuteQuery: 执行查询语句,返回DataTable(带参)

///

/// 执行查询语句,返回DataTable

///

/// 查询语句

/// DataTable

public DataTable ExecuteQuery(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

SqlCommand cmd = new SqlCommand();

PrepareCommand(cmd, con, null, sql, parameters);

using (SqlDataAdapter da = new SqlDataAdapter(cmd))

{

DataTable table = new DataTable();

try

{

(table);

();

}

catch (SqlException ex)

{

throw new Exception(e);

}

return table;

}

}

}

#endregion

#region PrepareCommand: 处理参数

private void PrepareCommand(SqlCommand cmd, SqlConnection con, SqlTransaction tran, string

cmdText, SqlParameter[] parameters) {

if ( != )

();

tion = con;

dText = cmdText;

if (tran != null)

ction = tran;

dType = ;

if (parameters != null)

{

foreach (SqlParameter parameter in parameters)

{

if ((ion == utput || ion ==

) && ( == null))

{

= ;

}

(parameter);

}

}

}

#endregion

#region ExecuteScalar: 执行一条计算查询结果语句,返回查询结果object(带参)

///

/// 执行一条计算查询结果语句,返回查询结果(object)。

///

/// 计算查询结果语句

/// 查询结果(object)

public object ExecuteScalar(string sql, params SqlParameter[] parameters)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand())

{

try

{

PrepareCommand(cmd, con, null, sql, parameters);

object obj = eScalar();

();

if (((obj, null)) || ((obj, )))

{

return null;

} else

{

return obj;

}

}

catch (SqlException e)

{

throw e;

}

}

}

}

#endregion

#region ExecuteScalar: 执行一条计算查询结果语句,返回查询结果object

///

/// 执行一条计算查询结果语句,返回查询结果(object)。

///

/// 计算查询结果语句

/// 查询结果(object)

public object ExecuteScalar(string sql)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlCommand cmd = new SqlCommand(sql, con))

{

try

{

();

object obj = eScalar();

if (((obj, null)) || ((obj, )))

{

return null;

}

else

{

return obj;

}

}

catch (SqlException e)

{

();

throw e;

} }

}

}

#endregion

#region GetDs:执行一条计算查询结果语句,返回查询结果DataSet

///

/// 返回DataSet数据集

///

/// SQL语句

public DataSet GetDs(string strSql)

{

DataSet ds;

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlDataAdapter sda = new SqlDataAdapter(strSql, con))

{

try

{

();

ds = new DataSet();

(ds);

}

catch (SqlException e)

{

();

throw e;

}

finally

{

();

}

}

}

return ds;

}

#endregion

#region GetDs:执行一条计算查询结果语句,返回查询结果装载到参数DataSet中,无返回值

///

/// 添加DataSet表

///

/// DataSet对象

/// Sql语句 /// 表名

public void GetDs(DataSet dss, string strSql, string strTableName)

{

using (SqlConnection con = new SqlConnection(SqlConnectionString))

{

using (SqlDataAdapter sda = new SqlDataAdapter(strSql, con))

{

try

{

();

(dss, strTableName);

}

catch (SqlException e)

{

();

throw e;

}

finally

{

();

}

}

}

}

#endregion

/////

///// 执行不带参数的增删改SQL语句或存储过程

/////

///// 增删改SQL语句或存储过程

///// 命令类型

/////

//public int ExecuteNonQuery(string cmdText, CommandType ct)

//{

// int res;

// try

// {

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// res = eNonQuery();

// }

// catch (Exception ex)

// { // throw ex;

// }

// finally

// {

// if ( == )

// {

// ();

// }

// }

// return res;

//}

/////

///// 执行带参数的增删改SQL语句或存储过程

/////

///// 增删改SQL语句或存储过程

///// 命令类型

///// int值

//public int ExecuteNonQuery(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// int res;

// using (cmd = new SqlCommand(cmdText, GetConn()))

// {

// dType = ct;

// ge(paras);

// res = eNonQuery();

// }

// return res;

//}

/////

///// 执行查询SQL语句或存储过程

/////

///// 查询SQL语句或存储过程

///// 命令类型

///// Table值

//public DataTable ExecuteQuery(string cmdText, CommandType ct)

//{

// DataTable dt = new DataTable();

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// using (sdr = eReader(onnection))

// {

// (sdr); // }

// return dt;

//}

/////

///// 执行带参数的查询SQL语句或存储过程

/////

///// 查询SQL语句或存储过程

///// 参数集合

///// 命令类型

///// Table值

//public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// DataTable dt = new DataTable();

// cmd = new SqlCommand(cmdText, GetConn());

// dType = ct;

// ge(paras);

// using (sdr = eReader(onnection))

// {

// (sdr);

// }

// return dt;

//}

/////

///// 执行带参数的Scalar查询

/////

///// 查询SQL语句或存储过程

///// 参数集合

///// 命令类型

///// 一个int型值

//public int ExecuteCheck(string cmdText, SqlParameter[] paras, CommandType ct)

//{

// int result;

// using (cmd = new SqlCommand(cmdText, GetConn()))

// {

// dType = ct;

// ge(paras);

// result = 32(eScalar());

// }

// return result;

//}

}

}