本文封转ado.net的常用操作,若是项目中用到此工具类,直接copy过去即可,省得每次开发都做同样的事情,当然了,用orm框架是另一种选择,这里不做过多讨论
///
/// 使用ado.net无外乎,文本,存储过程,事务,本文分别做了封装
/// 使用者直接复制进自己的项目持久层(DAL),在表现层项目配置文件中配置自己的连接字符串name为ConnStr即可调用
/// 用户还可以自己封装成接口,或者使用依赖注入的方式提供给业务逻辑层(BLL)
///
public class SqlHelper
{
//连接字符串的那么为ConnStr
private static readonly string ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
private static readonly string ConnStrExtend = ConfigurationManager.ConnectionStrings["ConnStrExtend"].ConnectionString;
private static readonly SqlConnection SqlConn = new SqlConnection(ConnStr);
private static readonly SqlConnection SqlConnExtend = new SqlConnection(ConnStrExtend );
private SqlHelper()
{
}
///
/// 封装ado.net的ExecQuery
///
///
///
///
public static DataTable ExecQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(sql, SqlConn);
if (paramers != null)
{
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
}
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
///
/// 封装ado.net的ExecScalar
///
///
///
///
public static object ExecScalar(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteScalar();
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
///
/// 封装ado.net的ExecNonQuery
///
///
///
///
public static int ExecNonQuery(string sql, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(sql, SqlConn);
if (paramers != null)
{
sqlCommand.Parameters.AddRange(paramers);
}
sqlCommand.CommandType = CommandType.Text;
SqlConn.Open();
return sqlCommand.ExecuteNonQuery();
}
catch(Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
///
/// 封装ado.net的存储过程的更新操作
///
///
///
///
public static int ExecStoredProcedureAsUpdate(string name, SqlParameter[] paramers)
{
try
{
var sqlCommand = new SqlCommand(name, SqlConn);
sqlCommand.Parameters.AddRange(paramers);
sqlCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
sqlCommand.ExecuteNonQuery();
return int.Parse(sqlCommand.Parameters["@return"].Value.ToString());
}
catch (Exception e)
{
return int.MinValue;
}
finally
{
SqlConn.Close();
}
}
///
/// 封装ado.net的存储过程的查询操作
///
///
///
///
public static DataTable ExecStoredProcedureAsQuery(string name, SqlParameter[] paramers)
{
try
{
var sqlDataAdapter = new SqlDataAdapter(name, SqlConn);
sqlDataAdapter.SelectCommand.Parameters.AddRange(paramers);
sqlDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
sqlDataAdapter.SelectCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
sqlDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlConn.Open();
var dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
return dataTable;
}
catch (Exception e)
{
return null;
}
finally
{
SqlConn.Close();
}
}
///
/// 封装ado.net的事务的更新操作,不能跨越多个数据库连接
///
///
///
///
public static int[] ExecTransactionAsUpdate(string[] sql, List
{
if (sql.Length == paramers.Count)
{
int[] i = new int[sql.length];
int rowCount = 0;//影响的行数
SqlTransaction sqlTransaction = SqlConn.BeginTransaction();
var sqlCommand = new SqlCommand { Transaction = sqlTransaction, Connection = SqlConn,CommandType = CommandType.Text};
SqlConn.Open();
try
{
for (int i = 0; i < sql.Length; i++)
{
sqlCommand.CommandText = sql[i];
if (paramers[i] != null)
{
sqlCommand.Parameters.AddRange(paramers[i]);
}
i[i] = sqlCommand.ExecuteNonQuery();
}
sqlTransaction.Commit();
return i;
}
catch (Exception e)
{
sqlTransaction.Rollback();
return null;
}
finally
{
SqlConn.Close();
}
}
return null;
}
}
///
/// 封装ado.net的事务的更新操作,可以跨越两个数据库连接
///
///
///
///
///
///
///
///
public static bool ExecTransactionAsUpdate(string[] sqlConnStr, List
try{
using (TransactionScope cope = new TransactionScope())
{
if(sqlConnStr.length == paramersConnStr.count){
SqlConn.Open();
var sqlCommand = new SqlCommand {Connection = SqlConn,CommandType = CommandType.Text};
for (int i = 0; i < sqlConnStr.Length; i++)
{
sqlCommand.CommandText = sqlConnStr[i];
if (paramersConnStr[i] != null)
{
sqlCommand.Parameters.AddRange(paramersConnStr[i]);
}
i[i] = sqlCommand.ExecuteNonQuery();
}
}
if(sqlConnStrExtend.length == paramersConnStrExtend.count){
SqlConnExtend.Open();
var sqlCommand = new SqlCommand {Connection = ConnStrExtend,CommandType = CommandType.Text};
for (int i = 0; i < sqlConnStrExtend.Length; i++)
{
sqlCommand.CommandText = sqlConnStrExtend[i];
if (paramersConnStr[i] != null)
{
sqlCommand.Parameters.AddRange(paramersConnStrExtend[i]);
}
j[i] = sqlCommand.ExecuteNonQuery();
}
}
cope.Complete();
}
return true;
}catch(Exception e){
return false;
}finally{
SqlConn.Close();
SqlConnExtend .close();
}
return false;
}