看看源程序大家应该知道怎么做可以解决这个问题了.
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.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();
}
}
在下抛一砖,希望能引玉无数.