(笔记)将指定数组中的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();
}
}