关于强类型Dataset的用法和好处,我就不再多说,网上关于这方面的资料很多 , 感兴趣的话可以在GoOGLE搜一下。
我们直奔主题,好处是很多,但若使用事务的话就不方便了。最近通过查找国外的资料,总于找到解决的方法。经过自己测试发现很好用,所以把代码贴出来,给正处于这方面困惑的朋友解答:
首先写一个类文件,代码如下:
public class HelperTA
{
public static SqlTransaction BeginTransaction(object tableAdapter)
{
return BeginTransaction(tableAdapter, IsolationLevel.ReadUncommitted);
}
public static SqlTransaction BeginTransaction(object tableAdapter, IsolationLevel isolationLevel)
{
// get the table adapter's type
Type type = tableAdapter.GetType();
// get the connection on the adapter
SqlConnection connection = GetConnection(tableAdapter);
// make sure connection is open to start the transaction
if (connection.State == ConnectionState.Closed)
connection.Open();
// start a transaction on the connection
SqlTransaction transaction = connection.BeginTransaction(isolationLevel);
&nb

本文介绍了如何在使用强类型Dataset时进行事务处理。通过创建一个名为HelperTA的辅助类,提供BeginTransaction方法来开始事务,并设置SqlTransaction到TableAdapter的相关SqlCommand。在事务的try-catch-finally块中,实现数据插入并根据执行结果决定提交或回滚事务。

174

被折叠的 条评论
为什么被折叠?



