/// <summary> /// 执行一条非查询语句,返回受影响的行数 /// </summary> public int Execute(string sql) { int i = 0; SqlConnection conn = new SqlConnection(sqlConnString); try { conn.Open(); } catch { DBErrorLog.SaveLog("数据库打开错误:\r\n" + sqlConnString + "\r\n"); throw; } try { SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = 120; i = cmd.ExecuteNonQuery(); } catch { DBErrorLog.SaveLog("数据库执行错误:\r\n" + sql + "\r\n"); throw; } finally { conn.Close(); } return(i); }
/// <summary> /// 多事务处理 /// </summary> /// <returns></returns> public bool multiTransation(List <TransactionBody> Arr) { SqlConnection conn = new SqlConnection(sqlConnString); string sql = ""; try { conn.Open(); } catch { throw; } SqlTransaction trans = conn.BeginTransaction(); try { for (int i = 0; i < Arr.Count; i++) { sql += "执行语句:" + ((TransactionBody)Arr[i]).SSQLText; SqlCommand cmd = new SqlCommand(((TransactionBody)Arr[i]).SSQLText, conn, trans); cmd.CommandTimeout = 120; if (((TransactionBody)Arr[i]).Parameters != null && ((TransactionBody)Arr[i]).Parameters.Length > 0) { foreach (SqlParameter p in ((TransactionBody)Arr[i]).Parameters) { cmd.Parameters.Add(p); sql += "|参数:" + p.Value; } } cmd.ExecuteNonQuery(); } trans.Commit(); } catch { DBErrorLog.SaveLog("数据库执行错误:\r\n" + sql + "\r\n"); trans.Rollback(); throw; } finally { conn.Close(); } return(true); }
/// <summary> /// 单条数据操作数据库(增、删、改) /// </summary> /// <param name="strSQL">SQL语句</param> /// <param name="ds">Dataset</param> /// <returns></returns> public int ExecuteDataSet(string strSQL, DataSet ds) { int i = 0; SqlConnection conn = new SqlConnection(sqlConnString); try { conn.Open(); } catch { throw; } SqlTransaction trans = conn.BeginTransaction(); try { SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(strSQL, conn, trans); da.InsertCommand = new SqlCommand("", conn, trans); da.UpdateCommand = new SqlCommand("", conn, trans); da.DeleteCommand = new SqlCommand("", conn, trans); SqlCommandBuilder cb = new SqlCommandBuilder(da); da.InsertCommand = cb.GetInsertCommand(); da.UpdateCommand = cb.GetUpdateCommand(); da.DeleteCommand = cb.GetDeleteCommand(); da.Update(ds); trans.Commit(); i = 1; } catch { DBErrorLog.SaveLog("数据库执行错误:\r\n" + strSQL + "\r\n"); trans.Rollback(); throw; } finally { conn.Close(); } return(i); }
//string connString = GetConnectionString(); // string sqlConnString; ///// <summary> ///// 获取数据库连接字符串 ///// </summary> ///// <returns></returns> //public static string GetConnectionString() //{ // string connString = ""; // try // { // if (connString.Equals("")) // { // sqlConnString = System.Configuration.ConfigurationManager.ConnectionStrings["XCCloudDB"].ToString(); // } // } // catch // { // DBErrorLog.SaveLog("数据库连接错误:\r\n" + connString + "\r\n"); // throw; // } // return sqlConnString; //} /// <summary> /// 数据库查询 /// </summary> /// <param name="strSQL">查询语句</param> /// <returns></returns> public DataSet ExecuteQuery(string strSQL) { SqlConnection conn = new SqlConnection(sqlConnString); try { conn.Open(); } catch { DBErrorLog.SaveLog("数据库打开错误:\r\n" + sqlConnString + "\r\n"); throw; } SqlTransaction trans = conn.BeginTransaction(); if (strSQL.Equals("")) { return(null); } DataSet ds = new DataSet(); try { SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = new SqlCommand(strSQL, conn, trans); da.SelectCommand.CommandTimeout = 120; da.Fill(ds); trans.Commit(); } catch { trans.Rollback(); DBErrorLog.SaveLog("数据库执行错误:\r\n" + strSQL + "\r\n"); throw; } finally { conn.Close(); } return(ds); }