| | |
| | | |
| | | namespace Pub_Class |
| | | { |
| | | public class ClsSqlHelper:IDisposable |
| | | public class ClsSqlHelper : IDisposable |
| | | { |
| | | private SqlConnection MainCn;//创建连接对象 |
| | | public string strCon; |
| | | SqlTransaction MainTran; |
| | | public string sServer; |
| | | public string sDataBase ; |
| | | public string sDataBase; |
| | | public string sUser; |
| | | public string sPassword; |
| | | //public string sTextBox = "HXErp_Test.ini"; |
| | | public string sTextBox = "HXErp.ini"; |
| | | //开始事务 |
| | | public void BeginTran() |
| | | public void BeginTran() |
| | | { |
| | | if (!this.CnOpen()) |
| | | return; |
| | | MainTran=MainCn.BeginTransaction(); |
| | | MainTran = MainCn.BeginTransaction(); |
| | | } |
| | | //结束事务 |
| | | public void Commit() |
| | |
| | | ClsPub.AppPath = System.Environment.CurrentDirectory; |
| | | //ClsPub.AppPath = @"C:\Windows\System32"; |
| | | } |
| | | sServer = ClsIni.ReadIni("Erp", "Server", ClsPub.AppPath + @"\"+sTextBox); |
| | | sServer = ClsIni.ReadIni("Erp", "Server", ClsPub.AppPath + @"\" + sTextBox); |
| | | sDataBase = ClsIni.ReadIni("Erp", "DataBase", ClsPub.AppPath + @"\" + sTextBox); |
| | | sUser = ClsIni.ReadIni("Erp", "UserName", ClsPub.AppPath + @"\" + sTextBox); |
| | | sPassword = ClsIni.ReadIni("Erp", "PassWord", ClsPub.AppPath + @"\" + sTextBox); |
| | |
| | | |
| | | public bool CheckOpen() |
| | | { |
| | | if (sServer == "" || sServer == "没有找到!" ) |
| | | if (sServer == "" || sServer == "没有找到!") |
| | | { |
| | | throw new Exception("错误的服务器名!!" + System.Environment.CurrentDirectory); |
| | | } |
| | | if (sDataBase == "" || sDataBase == "没有找到!") |
| | | { |
| | | throw new Exception("错误的数据库名!!" +System.Environment.CurrentDirectory); |
| | | throw new Exception("错误的数据库名!!" + System.Environment.CurrentDirectory); |
| | | } |
| | | SqlConnection oCn = new SqlConnection("Data Source=" + sServer + ";DataBase=master;User ID=" + sUser + ";PWD=" + sPassword + ";max pool size=32767"); |
| | | try |
| | |
| | | } |
| | | } |
| | | |
| | | private bool CnOpen()//OPEN数据库连接 |
| | | private bool CnOpen()//OPEN数据库连接 |
| | | { |
| | | if (sServer == "" || sServer == "没有找到!") |
| | | { |
| | |
| | | { |
| | | throw new Exception("错误的数据库名!"); |
| | | } |
| | | if (MainCn==null) |
| | | if (MainCn == null) |
| | | { |
| | | MainCn = new SqlConnection("Data Source=" + sServer + ";DataBase=" + sDataBase + ";User ID=" + sUser + ";PWD=" + sPassword + ";max pool size=32767"); |
| | | } |
| | |
| | | |
| | | public void CnDispose()//释放资源 |
| | | { |
| | | if(MainCn!=null) |
| | | { |
| | | if (MainCn != null) |
| | | { |
| | | MainCn.Dispose(); |
| | | MainCn=null; |
| | | MainCn = null; |
| | | } |
| | | } |
| | | /// <summary> |
| | |
| | | /// <returns>新的 parameter 对象</returns> |
| | | public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) |
| | | { |
| | | return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); |
| | | return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value); |
| | | } |
| | | /// <summary> |
| | | /// 初始化参数值 |
| | |
| | | /// <param name="procName"></param> |
| | | /// <param name="sErrMessage"></param> |
| | | /// <returns></returns> |
| | | public void RunProc(string procName,ref string sErr) |
| | | public void RunProc(string procName, ref string sErr) |
| | | { |
| | | if (!this.CnOpen()) |
| | | { |
| | | sErr = "连接数据库失败!"; |
| | | return; |
| | | |
| | | |
| | | } |
| | | try |
| | | { |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | sErr =e.Message; |
| | | sErr = e.Message; |
| | | ClsPub.sErrInfo = e.Message; |
| | | throw (e); |
| | | //return; |
| | |
| | | /// <param name="tbName">表名</param> |
| | | /// <param name="sErrMessage">异常信息</param> |
| | | /// <returns></returns> |
| | | public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName ) |
| | | public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) |
| | | { |
| | | SqlDataAdapter dap = CreateDataAdaper(procName, prams); |
| | | if (dap == null) |
| | |
| | | /// <param name="tbName">表名</param> |
| | | /// <param name="sErrMessage">异常信息</param> |
| | | /// <returns></returns> |
| | | public DataSet RunProcReturn(string procName, string tbName ) |
| | | public DataSet RunProcReturn(string procName, string tbName) |
| | | { |
| | | SqlDataAdapter dap = CreateDataAdaper(procName, null); |
| | | DataSet ds = new DataSet(); |
| | |
| | | /// <param name="tbName">表名</param> |
| | | /// <param name="sErrMessage">异常信息</param> |
| | | /// <returns></returns> |
| | | public DataSet RunProcReturn(string procName, string tbName,ref string sErr) |
| | | public DataSet RunProcReturn(string procName, string tbName, ref string sErr) |
| | | { |
| | | SqlDataAdapter dap = CreateDataAdaper(procName, null); |
| | | DataSet ds = new DataSet(); |
| | |
| | | |
| | | } |
| | | |
| | | public SqlDataReader RunReader(string sSQL,string tbName,ref string sErr) |
| | | public SqlDataReader RunReader(string sSQL, string tbName, ref string sErr) |
| | | { |
| | | if (!this.CnOpen()) |
| | | { |
| | |
| | | catch (Exception e) |
| | | { |
| | | ClsPub.sErrInfo = e.Message; |
| | | sErr = e.Message; |
| | | sErr = e.Message; |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 批量插入数据库 |
| | | /// </summary> |
| | | /// <param name="TableName">目标表</param> |
| | | /// <param name="dt">源数据</param> |
| | | public bool SqlBulkCopyByDatatable(string TableName, DataTable dt) |
| | | { |
| | | string connectionString = $"Data Source={sServer};DataBase={sDataBase};User ID={sUser};PWD={sPassword}"; |
| | | using (SqlConnection conn = new SqlConnection(connectionString)) |
| | | { |
| | | using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction)) |
| | | { |
| | | try |
| | | { |
| | | sqlbulkcopy.DestinationTableName = TableName; |
| | | for (int i = 0; i < dt.Columns.Count; i++) |
| | | { |
| | | sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName); |
| | | } |
| | | sqlbulkcopy.WriteToServer(dt); |
| | | return true; |
| | | } |
| | | catch (System.Exception ex) |
| | | { |
| | | //ex.Message; |
| | | return false; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | #region IDisposable 成员 |
| | | |
| | | public void Dispose() |