IT技术网www.itjs.cn

当前位置:首页 > 数据库 > MySQL > 教您用事务一次处理多条SQL语句

教您用事务一次处理多条SQL语句

发布时间:2010-09-07 15:31 来源:未知

接下来的SQL server内容就将为您介绍如何使用事务一次处理多条SQL语句的方法,包括SQL中事务的写法以及调用的方法供您参考,希望对您学习SQL语句的执行能够有所帮助。

执行一个操作时,要同时修改多张表里的数据,并且每条数据都必须操作成功,才算执行成功,为了防止有些数据操作失败,有些成功,而造成的数据错乱,大家要使用事务。

事务就是:只有所有操作都成功则成功,只要有一条数据操作失败,则回滚。

后台写事务:

/// <summary>

        /// 执行多条SQL语句,实现数据库事务。

       /// </summary> access数据库

       /// <param name="SQLStringList">多条SQL语句</param>  

        public static void ExecuteSqlTran(List<string> SQLStringList)

        {

            using (OleDbConnection conn = new OleDbConnection(SqlHelper.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 (System.Data.OleDb.OleDbException E)

                {

                    tx.Rollback();

                    throw new Exception(E.Message);

                }

            }

        }

/// <summary>

        /// 执行多条SQL语句,实现数据库事务。

        /// </summary>sql2000数据库

        /// <param name="SQLStringList">多条SQL语句</param>

        public static void ExecuteSqlTran(List<string> 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.SqlClient.SqlException E)

                {

                    tx.Rollback();

                    throw new Exception(E.Message);

                }

            }

        }

前台调用:

try

{

    //1.查出会员余额

    decimal usermoney = SqlDal.EntityUserinfo.GetModel(int.Parse(cookieUserID)).Money;

    //2.修改余额

    decimal Zmoney = usermoney + moAD.Commission;

    //写SQL语句

    List<string> SQLStringList = new List<string>();

    string clickSql = "insert into [user] (name,age)values('" + 小名 + "','“+4岁+”')";

    string userSql = "update [class] set [name]='" + 幼儿园 + "' where id=" + 2 + " ";

    SQLStringList.Add(clickSql);

    SQLStringList.Add(userSql);

    SqlDal.SqlHelper.ExecuteSqlTran(SQLStringList);

//数据库操作成功

    //提示

    CommonClass.Xmls xmls1 = new CommonClass.Xmls();

    string path1 = CommonClass.Unit.GetMapPath(@"/Admin/Configs/SysSettingInfo.config");

    string ClickTishi = xmls1.GetXmlNode(path1, "SysSettingInfo/ClickTishi");

    //替换字符

    ClickTishi = ClickTishi.Replace("[$]", moAD.Commission.ToString("0.00"));

    context.Response.Write(ClickTishi); //输出

}

catch (Exception ex)

{

    //Response.Write(ex.Message);

    context.Response.Write("操作失败!" + ex.Message); //输出

}