最近在做.NET开发时,每个项目每次都要写数据库连接,CRUD的操作,效率很慢,在时间之余整理了一个通用类(DataHelper.cs)。希望帮助学习的人,同样哪里有错误请诸位指正(改不改我的事,哈哈哈)
→→→→→多了不说,少了不唠。进入正题→→→→
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
namespace APITool.Util
{
public class DataHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["APITool.Properties.Settings.SqlServerConnection"].ConnectionString;
DbProviderFactory provider;
public DataHelper()
{
provider = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["APITool.Properties.Settings.SqlServerConnection"].ProviderName);
}
///
/// 执行SQL语句,返回记录数
///
/// SQL语句
/// 影响的记录数
public int ExecuteSql(string SQLString)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
cmd.Connection = connection;
cmd.CommandText = SQLString;
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (DbException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// 多条SQL语句
public void ExecuteSqlTran(ArrayList SQLStringList)
{
using (DbConnection conn = provider.CreateConnection())
{
conn.ConnectionString = connectionString;
conn.Open();
using (DbCommand cmd = provider.CreateCommand())
{
cmd.Connection = conn;
using (DbTransaction tx = conn.BeginTransaction())
{
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (DbException ex)
{
tx.Rollback();
conn.Close();
conn.Dispose();
throw ex;
}
}
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object)。
///
/// 计算查询结果语句
/// 查询结果(object)
public object GetSingle(string SQLString)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
cmd.Connection = connection;
cmd.CommandText = SQLString;
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (DbException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
}
///
/// 执行查询语句,返回SqlDataReader
///
/// 查询语句
/// SqlDataReader
public DbDataReader ExecuteReader(string strSQL)
{
DbConnection connection = provider.CreateConnection();
connection.ConnectionString = connectionString;
DbCommand cmd = provider.CreateCommand();
cmd.Connection = connection;
cmd.CommandText = strSQL;
try
{
connection.Open();
DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.Common.DbException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
///
/// 执行查询语句,返回DataSet
///
/// 查询语句
/// DataSet
public DataSet GetDataSet(string SQLString)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
cmd.Connection = connection;
cmd.CommandText = SQLString;
try
{
DataSet ds = new DataSet();
DbDataAdapter adapter = provider.CreateDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds, "ds");
return ds;
}
catch (DbException ex)
{
connection.Close();
connection.Dispose();
throw new Exception(ex.Message);
}
}
}
}
///
/// 执行SQL语句,返回影响的记录数
///
/// SQL语句
/// 影响的记录数
public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
cmd.Connection = connection;
cmd.CommandText = SQLString;
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (DbException E)
{
connection.Close();
connection.Dispose();
throw new Exception(E.Message);
}
}
}
}
///
/// 执行多条SQL语句,实现数据库事务。
///
/// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
public void ExecuteSqlTran(Hashtable SQLStringList)
{
using (DbConnection conn = provider.CreateConnection())
{
conn.ConnectionString = connectionString;
conn.Open();
using (DbTransaction trans = conn.BeginTransaction())
{
using (DbCommand cmd = provider.CreateCommand())
{
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
DbParameter[] cmdParms = (DbParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch (DbException ex)
{
trans.Rollback();
conn.Close();
conn.Dispose();
throw ex;
}
}
}
}
}
///
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
///
/// 计算查询结果语句
/// 查询结果(object)
public object GetSingle(string SQLString, DbParameter[] cmdParms)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (DbException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
}
///
/// 执行查询语句,返回SqlDataReader
///
/// 查询语句
/// SqlDataReader
public DbDataReader ExecuteReader(string SQLString, DbParameter[] cmdParms)
{
DbConnection connection = provider.CreateConnection();
connection.ConnectionString = connectionString;
DbCommand cmd = provider.CreateCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
DbDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (DbException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
///
/// 执行查询语句,返回DataTable
///
/// 查询语句
/// DataTable
public DataTable GetDataSet(string SQLString, DbParameter[] cmdParms)
{
using (DbConnection connection = provider.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand cmd = provider.CreateCommand())
{
using (DbDataAdapter da = provider.CreateDataAdapter())
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds.Tables[0];
}
catch (DbException ex)
{
connection.Close();
connection.Dispose();
throw new Exception(ex.Message);
}
}
}
}
}
private void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (DbParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
}
}
如有侵权请联系我删除。
结束!!!!!
大鹏一日通风起 扶摇直上九万里