我不知道在C#中怎么用代码调用事务,请高手给我指点指点,谢谢啦!!!!!

解决方案 »

  1.   

    方法很多:
    一、用system.transactions
    二、用存储过程,把事务写在存储过程中,在代码中传递参数,调用存储过程。
    三、代码:public static int ExecuteSqlTran(List<String> SQLStringList)
            {
                using (SqlConnection conn = new SqlConnection(connectionString))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    SqlTransaction tx = conn.BeginTransaction();
                    cmd.Transaction = tx;
                    try
                    {
                        int count = 0;
                        for (int n = 0; n < SQLStringList.Count; n++)
                        {
                            string strsql = SQLStringList[n];
                            if (strsql.Trim().Length > 1)
                            {
                                cmd.CommandText = strsql;
                                count += cmd.ExecuteNonQuery();
                            }
                        }
                        tx.Commit();
                        return count;
                    }
                    catch
                    {
                        tx.Rollback();
                        return 0;
                    }
                }
            }
      

  2.   

     这是不是动软的代码啊,但是他的存储过程该怎么写,还有就是:
     using (SqlConnection conn = new SqlConnection(connectionString))  这一句是什么意思啊?为什么要用using啊?我不知道,请再给我说说,谢啦!!!!
      

  3.   

    using (SqlConnection conn = new SqlConnection(connectionString))
    {
      ...................
    }
    这是using的另外一种用法:新建conn对象,当出现异常或者执行结束后,销毁conn对象。
    这三种方法是三种独立的方法。
    List<string> sqlList = new List<string>();
    sqlList.Add("SQL语句一")
    sqlList.Add("SQL语句二")
    sqlList.Add("SQL语句三")
    sqlList.Add("SQL语句四")
    .....................ExecuteSqlTran(sqlList)。
    这是其中的一种用法,还有,比较简单的就是用:system.transactions,具体用法你在网上搜搜。