using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace Bn6.DBUtility { /// <summary> /// 数据操作类 /// </summary> public class DBHelper { IDbConnection con = null; IDbDataAdapter adpt = null; IDbCommand com = null; IDbTransaction tran = null; DataSet ds = null; public DBHelper() { SqlFactory.Dbfile = AppDomain.CurrentDomain.BaseDirectory + "dbconfig.xml"; this.con = SqlFactory.CreateConnection(); this.adpt = SqlFactory.CreateDataAdapter(); } private static DBHelper _instance; /// <summary> /// AccessHelper单例 /// </summary> public static DBHelper Instance { get { if (_instance == null) _instance = new DBHelper(); return _instance; } } /// <summary> /// 开启事务 /// </summary> private void BeginTransaction() { if (this.con.State == ConnectionState.Open) { this.tran = this.con.BeginTransaction(); this.com.Transaction = this.tran; } } /// <summary> /// 执行查询的方法 /// </summary> /// <param name="sqlText">T-SQL语句</param> /// <returns>DataTable</returns> public DataTable Select(string sqlText) { DataTable dt = null; try { this.com = this.con.CreateCommand(); this.com.CommandTimeout = 60; this.com.CommandText = sqlText; this.adpt.SelectCommand = this.com; this.ds = new DataSet(); this.adpt.Fill(ds); dt = this.ds.Tables[0]; } catch (Exception ex) { throw new Exception(ex.Message); } return dt; } /// <summary> /// 执行查询的方法 /// </summary> /// <param name="sqlText">T-SQL语句</param> /// <returns>DataTable</returns> public DataSet GetDataSet(string sqlText) { try { this.com = this.con.CreateCommand(); this.com.CommandTimeout = 60; this.com.CommandText = sqlText; this.adpt.SelectCommand = this.com; this.ds = new DataSet(); this.adpt.Fill(ds); } catch (Exception ex) { throw new Exception(ex.Message); } return this.ds; } /// <summary> /// 执行增、删、改的方法 /// </summary> /// <param name="sqlText">T-SQL语句</param> /// <returns>结果:等于0执行失败,大于0执行成功</returns> public int Modify(string sqlText) { int result = 0; try { this.com = this.con.CreateCommand(); this.com.CommandTimeout = 60; this.com.CommandText = sqlText; this.con.Open(); this.BeginTransaction(); result = this.com.ExecuteNonQuery(); this.tran.Commit(); } catch (Exception ex) { this.tran.Rollback(); throw new Exception(ex.Message); } finally { this.con.Close(); } return result; } /// <summary> /// 批量插入数据 /// </summary> /// <param name="sqlText">T-SQL语句</param> /// <returns>结果:等于0执行失败,大于0执行成功</returns> public int BatchInsert(string sqlText) { int result = 0; try { this.com = this.con.CreateCommand(); this.com.CommandTimeout = 90; string[] sql = sqlText.Split(';'); this.con.Open(); this.BeginTransaction(); for (int i = 0; i < sql.Length; i++) { this.com.CommandText = sql[i]; this.com.ExecuteNonQuery(); result++; } this.tran.Commit(); } catch (Exception ex) { this.tran.Rollback(); throw new Exception(ex.Message); } finally { this.con.Close(); } return result; } /// <summary> /// 返回首行首列,通常用于Count、Sum、Max、Min等函数返回的值 /// </summary> /// <param name="sqlText">T-SQL语句</param> /// <returns>字符串</returns> public string GetCount(string sqlText) { string value = ""; try { this.com = this.con.CreateCommand(); this.com.CommandTimeout = 60; this.com.CommandText = sqlText; this.con.Open(); object objResult = this.com.ExecuteScalar(); if (objResult != null || Convert.IsDBNull(objResult)) { value = objResult.ToString(); } } catch (Exception ex) { throw new Exception(ex.Message); } finally { this.con.Close(); } return value; } /// <summary> /// 返回用于批量导入数据的SqlBulkCopy实例 /// </summary> /// <returns></returns> public SqlBulkCopy GetSqlBulkCopy() { try { SqlFactory.Dbfile = AppDomain.CurrentDomain.BaseDirectory + "dbconfig.xml"; IDbConnection newConn = SqlFactory.CreateConnection(); SqlBulkCopy sbc=new SqlBulkCopy(newConn.ConnectionString); //每批次提交5000行数据 sbc.BatchSize = 5000; //延长超时时间 sbc.BulkCopyTimeout = 300; return sbc; } catch (Exception ex) { throw ex; } } } }
浙公网安备 33010602011771号