c# 一次提交多条sql语句

warning: 这篇文章距离上次修改已过566天,其中的内容可能已经有所变动。

c#一次性提交n条sql给数据库处理   

因为要根据不同的条件循环批量update set 一些 数据,如果直接for next中间execute的话比较耗时,但又不能用存储过程(传值不是很方便),故想到了trans来处理。以下为源码,希望对大家有用

  byError Q:302777528 2012-03-20 00:53

public static void executeSqlTran(List<string> sqlList )
        {
            using (SqlConnection conn = new SqlConnection(ConnectionStringKy))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                  try
                {
                    for (int n = 0; n < sqlList.Count; n++)
                    {
                        string strsql = sqlList[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);
                }
            }

        }

        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] parameterArray = (SqlParameter[])parmCache[cacheKey];
            if (parameterArray == null)
            {
                return null;
            }
            SqlParameter[] parameterArray2 = new SqlParameter[parameterArray.Length];
            int index = 0;
         
            {
                parameterArray2[index] = (SqlParameter)((ICloneable)parameterArray[index]).Clone();
                index++;
            }
            return "";
        }

none
最后修改于:2023年05月08日 07:55

评论已关闭