[英]How do you access data that has been inserted using a ADO.NET transaction?
我正在嘗試獲取已通過ADO.NET事務成功輸入到數據庫中的數據。
調用trans.Commit()后,似乎沒有辦法取回已提交的數據,因為在事務處理期間創建的所有標識列都是“虛擬的”,因為它是一個脫機數據集,直到提交為止
非常感謝
[編輯]
嗯,問題是,我無法進行重新選擇,因為除了作為事務一部分插入的數據的身份之外,我沒有別的唯一選擇。
我無法獲得最后輸入的項目,因為這是一個多用戶系統
書中的代碼示例,不是問題代碼,但足以說明我的需要:
using System.Data;
using System.Data.SqlClient;
namespace DataAdapterTransaction
{
class Program
{
private static string sqlConnectString = "Data Source=(local);" +
"Integrated security=SSPI;Initial Catalog=AdoDotNet35Cookbook;";
private static string sqlSelect = "SELECT * FROM DataAdapterTransaction";
static void Main(string[] args)
{
object[,] o1 = {{ "1", "field 1.1", "field 2.1" },
{ "2", "field 1.2", "field 2.2" }};
InsertRecords(o1);
object[,] o2 = {{ "3", "field 1.3", "field 2.3" },
{ null, "field 1.4", "field 2.4" }};
InsertRecords(o2);
// Retrieve and output the contents of the table
SqlDataAdapter daRead = new SqlDataAdapter(sqlSelect, sqlConnectString);
DataTable dtRead = new DataTable( );
daRead.Fill(dtRead);
Console.WriteLine("---TABLE DataAdapterTransaction---");
foreach (DataRow row in dtRead.Rows)
Console.WriteLine("Id = {0}\tField1 = {1}\tField2 = {2}",
row["Id"], row["Field1"], row["Field2"]);
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey( );
}
static void InsertRecords(object[,] o)
{
DataTable dt = new DataTable( );
SqlTransaction tran;
SqlConnection connection = new SqlConnection(sqlConnectString);
// Create a DataAdapter
SqlDataAdapter da = new SqlDataAdapter(sqlSelect, connection);
// Stop updating when an error is encountered for roll back.
da.ContinueUpdateOnError = false;
// Create CommandBuilder and generate updating logic.
SqlCommandBuilder cb = new SqlCommandBuilder(da);
// Create and fill a DataTable with schema and data
da.Fill(dt);
// Open the connection
connection.Open( );
// Begin a new transaction and assign it to the DataAdapter
tran = connection.BeginTransaction( );
da.SelectCommand.Transaction = tran;
// Add two rows that will succeed update
for (int i = 0; i <= o.GetUpperBound(0); i++)
{
dt.Rows.Add(new object[] { o[i, 0], o[i, 1], o[i, 2] });
Console.WriteLine(
"=> Row with [Id = {0}] added to DataTable.", o[i, 0]);
}
Console.WriteLine("=> Updating data source using DataAdapter.");
try
{
da.Update(dt);
tran.Commit( );
Console.WriteLine("\nTRANSACTION COMMIT.\n");
}
catch (Exception ex)
{
tran.Rollback( );
Console.WriteLine("\nTRANSACTION ROLLBACK.\n{0}\n", ex.Message);
}
finally
{
connection.Close( );
}
}
}
}
好的,所以我要做的只是在事務提交之后,我想獲得最后插入的行的(作用域)標識。
我的應用程序成功地更新了三個數據適配器,並將其作為事務的一部分,但是我很難看待最終提交的數據。 我可以選擇一個表並在其中查看,但這對於生產代碼來說確實不夠好。
SC
您可能只需要重新選擇數據。
聯機叢書說,您應該再次致電Fill來進行數據集更新:
http://msdn.microsoft.com/zh-CN/library/33y2221y(VS.71).aspx
我通常設置我的插入和更新命令,以便它們為我的數據表返回有效的數據行,並以此方式控制應用程序中行的更新。
是的,所以我應該這樣做:
// update datatable
da.Update(dt);
// commit updates
tran.Commit( );
// get the updated datatable
da.Fill(dt);
我假設所有身份列都會更新。
我去吧
SC
我了解您使用的是身份列,但是數據中是否有任何自然鍵可用於重新選擇?
(然后提出了“為什么使用身份”的問題,但這是另一個主題……)
不幸的是,不行,我無法使用自然鍵進行重新選擇...我一直受困於身份...在敲打頭部8小時后,我打算添加一個Guid字段,以使其能夠正常工作,但認為這違反了我的原則放棄!
SC
此MSDN本文介紹如何在調用DataAdapter的Update方法時取回標識值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.