/// <summary>
193 /// 执行数据库事务
194 /// </summary>
195 /// <param name="sqlStringList"></param>
196 public void ExecuteTransaction(System.Collections.ArrayList sqlStringList)
197 {
198 Connection.Open();
199 DbTransaction tran = Connection.BeginTransaction();
200 DbCommand Command = BuildInitCommand();
201 Command.Transaction = tran;
202 try
203 {
204 for (int i = 0; i < sqlStringList.Count; i++)
205 {
206 Command.CommandText = sqlStringList[i].ToString();
207 Command.ExecuteNonQuery();
208 }
209 tran.Commit();
210 Connection.Close();
211 }
212 catch (System.Data.Common.DbException E)
213 {
214 tran.Rollback();
215 throw new Exception(E.Message);
216 }
217 }
218 }
事务处理函数只能执行纯SQL语句,如果想同时执行纯sql语句和带参的SQL语句,则不行。该如何改。请高手们指教???
193 /// 执行数据库事务
194 /// </summary>
195 /// <param name="sqlStringList"></param>
196 public void ExecuteTransaction(System.Collections.ArrayList sqlStringList)
197 {
198 Connection.Open();
199 DbTransaction tran = Connection.BeginTransaction();
200 DbCommand Command = BuildInitCommand();
201 Command.Transaction = tran;
202 try
203 {
204 for (int i = 0; i < sqlStringList.Count; i++)
205 {
206 Command.CommandText = sqlStringList[i].ToString();
207 Command.ExecuteNonQuery();
208 }
209 tran.Commit();
210 Connection.Close();
211 }
212 catch (System.Data.Common.DbException E)
213 {
214 tran.Rollback();
215 throw new Exception(E.Message);
216 }
217 }
218 }
事务处理函数只能执行纯SQL语句,如果想同时执行纯sql语句和带参的SQL语句,则不行。该如何改。请高手们指教???
如果是拼接式SQL的话执行会出错的,只能是参数式。
拼接式就是空啦
string sql2 = "SELECT * from a";
ArrayList sqlStringList = new ArrayList();
sqlStringList.Add(sql1);
sqlStringList.Add(sql2);
ExecuteTransaction(sqlStringList);