(笔记)将指定数组中的N条SQL语句同步执行

protected static int dbExecuteSqls(string[] strSQLs)
        {
            SqlConnection myCn = new SqlConnection(strConn);           
            SqlCommand myCmd = new SqlCommand();           
            int j=strSQLs.Length;

            try
            {
                myCn.Open();               
            }
            catch(System.Data.SqlClient.SqlException e)
            {
                throw new Exception(e.Message);
            }
            SqlTransaction myTrans = myCn.BeginTransaction();

            try
            {                                           
                myCmd.Connection = myCn;               
                myCmd.Transaction = myTrans;

                foreach(string str in strSQLs)
                {
                    myCmd.CommandText = str;
                    myCmd.ExecuteNonQuery();
                }
                myTrans.Commit();
                return 0;
            }
            catch(System.Data.SqlClient.SqlException e)
            {           
                myTrans.Rollback();
                throw new Exception(e.Message);
            }
            finally
            {
                myCmd.Dispose();
                myCn.Close();
            }
        }

posted @ 2007-06-17 17:06  玉米疯收  阅读(259)  评论(0编辑  收藏  举报