下面就将为您介绍如何使用一次多条的,包括SQL中事务的写法以及调用的方法供您参考,希望对您学习SQL语句的能够有所 。
执行一个时,要同时多张表里的,并且每条数据都必须操作成功,才算执行成功,为了防止有些数据操作失败,有些成功,而造成的数据错乱,我们要使用事务 。
事务就是:只有所有操作都成功则成功,只要有一条数据操作失败,则回滚 。
后台写事务:
/// /// 执行多条SQL语句,实现事务 。 /// access数据库 ///多条SQL语句 pubc stc vo ExecuteTran(List SQLStringList) { using (OleDbConnection conn = new OleDbConnection(.ConString)) { conn.Open(); OleDbCommand cmd = new OleDbCommand(); cmd.Connection = conn; OleDbTransaction 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 (Syst..OleDb.OleDbException E) { tx.Rollback(); throw new Exception(E.Message); } } }
/// /// 执行多条SQL语句,实现数据库事务 。 /// sql数据库 ///多条SQL语句 public static void ExecuteSqlTran(List SQLStringList) { using (SqlConnection conn = new SqlConnection(SqlHelper.ConString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction 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 (System.Data.Sql.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } } }
前台调用:
try{ //1.查出会员余额 decimal money = SqlDal.EntityUserinfo.Get(int.Parse(cookieUserID)).Money; //2.修改余额 decimal Zmoney = usermoney + moAD.Commission; //写SQL语句 List SQLStringList = new List(); string clickSql = "i into [user] (name,age)values(" + 小名 + ",“+4岁+”)"; string userSql = "up [] set [name]=" + 幼儿园 + " where id=" + 2 + " "; SQLStringList.d(clickSql); SQLStringList.Add(userSql); SqlDal.SqlHelper.ExecuteSqlTran(SQLStringList);
//成功 //提示 CommonClass.s xmls1 = new CommonClass.Xmls(); string path1 = CommonClass.Unit.GetMapPath(@"/Admin/s/SysSettingInfo.config"); string ClickTishi = xmls1.GetXmlNode(path1, "SysSettingInfo/ClickTishi"); //替换字符 ClickTishi = ClickTishi.Replace("[$]", moAD.Commission.ToString("0.00")); context..Write(ClickTishi); //输出}catch (Exception ex){ //Response.Write(ex.Message); context.Response.Write("操作失败!" + ex.Message); //输出}
下面将为您介绍如何多条,实现事务的,供您参考,如果您语句和事务感兴趣的话,不妨一看,详细对您学习SQL大有 。
///
/// 执行多条SQL语句,实现数据库事务 。 /// ///多条SQL语句 pubc stc vo ExecuteTran(IList SQLStringList) { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; SqlTransaction 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 (Syst..Sql.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } } }
protected void btnOk_Click(object sender, EventArgs e)
{ string upsql = "up 表 set=123 where id=";//省略其他SET IList l = new List(); for (int i = 0; i CheckBox c= (CheckBox)this.DataList1.Items[i].Find("CheckBox1"); TextBox tb = (TextBox)this.DataList1.Items[i].FindControl("TextBox1"); //下面几个TextBox省略 if(c.Checked) { l.d("update 表 set="+tb.Text+" where id="+ this.DataList1.DataKeys[i].ToString()); } } SqlHelper.ExecuteSqlTran(l); }