波斯马BOSSMA Information Technology

ASP.NET之Transact-SQL数据库事务

发布时间:2008年7月21日 / 分类:ASP.NET, DataBase / 10,013 次浏览 / 评论

好像只能应用于SQLServer,其它数据库未做测试。

using System.Data;
using System.Data.SqlClient;

???????? /// <summary>
???????? /// 执行多条SQL语句,实现数据库事务。
????? /// </summary>
??????? /// <param name="SQLStringList">多条SQL语句</param>??
??????? public int ExecuteSqlTran(ArrayList SQLStringList)
??????? {
??????????? int results = 0;
??????????? using (SqlConnection conn = new SqlConnection(connectionString))
??????????? {
??????????????? conn.Open();
??????????????? SqlCommand cmd = new SqlCommand();
??????????????? cmd.Connection = conn;
??????????????? SqlTransaction tx = conn.BeginTransaction();
??????????????? cmd.Transaction = tx;
??????????????? try
??????????????? {
??????????????????? for (int n = 0; n < SQLStringList.Count; n++)
??????????????????? {
??????????????????????? string strsql = SQLStringList[n].ToString();
??????????????????????? if (strsql.Trim().Length > 1)
??????????????????????? {
??????????????????????????? cmd.CommandText = strsql;
??????????????????????????? AddSQLLog(strsql);
??????????????????????????? cmd.ExecuteNonQuery();
??????????????????????? }
??????????????????? }
??????????????????? tx.Commit();
??????????????????? results = 1;
??????????????? }
??????????????? catch
??????????????? {
??????????????????? tx.Rollback();
??????????????????? results = 0;
??????????????? }
??????????????? finally
??????????????? {
??????????????????? cmd.Dispose();
??????????????????? conn.Close();
??????????????? }
??????????????? return results;
??????????? }
??????? }
本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自波斯马,原文地址《ASP.NET之Transact-SQL数据库事务

关键字:

建议订阅本站,及时阅读最新文章!
【上一篇】 【下一篇】

发表评论