关于VS2005中自动生成TableAdapter的事务处理
这里有个方法 http://www.developpez.net/forums/viewtopic.php?p=2527088
看看源程序大家应该知道怎么做可以解决这个问题了.
using System;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.DataSet1TableAdapters

{
partial class Table1TableAdapter
{
public SqlDataAdapter MyAdapter
{
get
{
return this.Adapter;
}
}
}

partial class Table2TableAdapter

{

public SqlDataAdapter MyAdapter

{

get
{
return this.Adapter;
}

}

}

}

// Here is the method to implement the Transaction:

private void ExecuteTransaction()

{

// The Connection must be identical in order to apply Transaction

SqlConnection connection = table1TableAdapter.Connection;
table2TableAdapter.Connection = connection;

// Start a local Transaction

SqlTransaction transaction = connection.BeginTransaction();

table1TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;

table2TableAdapter.MyAdapter.InsertCommand.Transaction = transaction;

try

{
// Update Database
table1TableAdapter.Update(dataSet1.Table1);
table2TableAdapter.Update(dataSet1.Table2);
// Commit Changes to database
transaction.Commit();
}

catch (DataException ex)

{

// Roll back the transaction.

if (transaction != null)

{

transaction.Rollback();

}

Console.WriteLine("Message: {0}", ex.Message);

}
finally
{

// Close Conection
connection.Close();

}

}
但是,这个写法,还是太傻瓜了,
在下抛一砖,希望能引玉无数.
看看源程序大家应该知道怎么做可以解决这个问题了.
using System;
using System.Data;
using System.Data.SqlClient; 
namespace DataAccess.DataSet1TableAdapters 
{
partial class Table1TableAdapter
{
public SqlDataAdapter MyAdapter
{
get
{
return this.Adapter;
}
}
} 
partial class Table2TableAdapter 
{ 
public SqlDataAdapter MyAdapter 
{ 
get
{
return this.Adapter;
} 
} 
} 
} 
// Here is the method to implement the Transaction: 
private void ExecuteTransaction() 
{ 
// The Connection must be identical in order to apply Transaction 
SqlConnection connection = table1TableAdapter.Connection;
table2TableAdapter.Connection = connection; 
// Start a local Transaction 
SqlTransaction transaction = connection.BeginTransaction(); 
table1TableAdapter.MyAdapter.InsertCommand.Transaction = transaction; 
table2TableAdapter.MyAdapter.InsertCommand.Transaction = transaction; 
try 
{
// Update Database
table1TableAdapter.Update(dataSet1.Table1);
table2TableAdapter.Update(dataSet1.Table2);
// Commit Changes to database
transaction.Commit();
} 
catch (DataException ex) 
{ 
// Roll back the transaction. 
if (transaction != null) 
{ 
transaction.Rollback(); 
} 
Console.WriteLine("Message: {0}", ex.Message); 
}
finally
{ 
// Close Conection
connection.Close(); 
} 
} 
在下抛一砖,希望能引玉无数.



浙公网安备 33010602011771号