数据库操作类如何实现对事务的封装?

fihuang 2009-09-19 11:42:02
重新开贴说明下一下情况!
现在我写了一个数据库读写操作类DBBase:

public Class DBBase
{
string connectionString = "***";

//操作sql语句
public bool ExecuteSql(string sql)
{
bool result = false;

SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(sql,connection);
connection.Open();
int i = Convert.ToInt32(cmd.ExecuteNonQuery());
if(i == 1)
result = true;

if(cmd != null)
{
cmd.Cancel();
cmd.Dispose();
}
if(connection != null)
{
connection.Close();
connection.Dispose();
}

return result;
}
}

//调用DBBase
DBBase db = new DBBase();
string sql = "delete from tableUser where id = 1";
bool result = db.ExecuteSql();


如果参数是一条sql语句,可以直接调用DBBase的ExecuteSql方法。但如果是事务,添加一个操作事务的方法,这个方法应该怎么写?将什么作为参数传入?
不知道这回说清楚了没有?
...全文
117 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
天乐 2009-09-21
  • 打赏
  • 举报
回复
关注
fihuang 2009-09-21
  • 打赏
  • 举报
回复
周末没人啊 继续顶
wangan2008 2009-09-19
  • 打赏
  • 举报
回复
up
wuyq11 2009-09-19
  • 打赏
  • 举报
回复
看看sqlhelper,操作数据库很详细
wuyq11 2009-09-19
  • 打赏
  • 举报
回复
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
transaction = connection.BeginTransaction("SampleTransaction");
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText ="";
command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();

}
}
}
fihuang 2009-09-19
  • 打赏
  • 举报
回复
我例子中的方法封装了sql语句,我的问题是在这个类中添加一个封装事务的方法,请问怎么实现?
fihuang 2009-09-19
  • 打赏
  • 举报
回复
我看到过这样一个方法:

public void ExecuteTran(string[] sqls)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;
transaction = connection.BeginTransaction(connection);

try
{
conn.Open();
for(int i = 0;i < sqls.Length;i ++)
{
cmdmand.CommandText = sqls[i];
cmdmand.ExecuteNonQuery();
}
transaction.Commit();
}
catch()
{
transaction.Rollback();
}
}

这个方法不错,但是再下面2种情况下怎么办?
1.sql语句带参数,SqlParameter[] 。
2.比如这个事务有10条sql语句,但是第9、10条是根据第8条的结果来决定是否执行的。如果第8条返回结果大于100,那么执行第9、10条sql语句,否则不执行。
fihuang 2009-09-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
看看sqlhelper,操作数据库很详细

[/Quote]sqlhelper看过,有时候觉得不是很好用。

[Quote=引用 2 楼 wuyq11 的回复:]
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
transaction = connection.BeginTransaction("SampleTransaction");
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText ="";
command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();

}
}
}

[/Quote]
我不太明白你这个方法,SampleTransaction这个是什么?

这样吧,我说一个实际的例子:
表1:student(id,name):
1 刘德华
2 张学友
3 郭富城

表2:course(id,name):
1 数学
2 英语
3 语文

表3:score(studentID,courseID,score)
1 1 98
1 2 85
1 3 99
2 1 95
2 2 90
2 3 70
3 1 90
3 2 60
3 3 100

现在要删除数学这一门课,那么要先删除score表中数据成绩的数据,再删除course中数学这么科目。
string sql_1 = "delete from score where courseID = (select id from course where name = '数学')";
string sql_2 = "delete from course where name = '数学'";
现在要先执行sql_1,再执行sql_2,所有要用事务(不讨论存储过程)。我要在数据库操作类DBBase里写一个为事务服务的方法,请问怎么实现?

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧