[英]ADO.NET DataAdapters - Ensuring one Table is Update Before Another
我正在开发一个应用程序,当用户单击“提交”按钮时,该数据将数据插入两个单独但相关的表中。
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_details_report"
但是,由于我在一张桌子上有外键约束,所以我一直遇到困难。 由于外键限制,我需要先插入一个表(report_summary)的行,然后再将一行添加到另一表(report_details)。 但是,我也希望它们在单个事务中处理,一个插入可能成功而另一个插入失败可能会出现一些数据完整性问题。 我该如何解决?
T-SQL
CREATE TABLE [dbo].[report_summary] (
[report_id] INT NOT NULL,
[inspector] INT NOT NULL,
[employee] INT NOT NULL,
[room] INT NOT NULL,
[date] DATE NOT NULL,
[score] INT NOT NULL,
[locationID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([report_id] ASC),
CONSTRAINT [FK_report_summary_locations] FOREIGN KEY ([locationID]) REFERENCES [dbo].[locations] ([locID])
);
CREATE TABLE [dbo].[report_details] (
[reportID] INT NOT NULL,
[itemID] INT NOT NULL,
[points] INT NOT NULL,
[comments] NTEXT NULL,
PRIMARY KEY CLUSTERED ([itemID] ASC, [reportID] ASC),
CONSTRAINT [FK_details_items] FOREIGN KEY ([itemID]) REFERENCES [dbo].[items] ([itemID]),
CONSTRAINT [FK_details_report] FOREIGN KEY ([reportID]) REFERENCES [dbo].[report_summary] ([report_id])
);
和我的一些C#
private void submitData(object sender, RoutedEventArgs e)
{
SqlTransaction tran = con.BeginTransaction();
reportAdapter.InsertCommand.Transaction = tran;
SqlCommand query = new SqlCommand("SELECT report_id FROM dbo.report_summary ORDER by report_id DESC", con);
query.Transaction = tran;
int nextReportID;
if (query.ExecuteScalar() != null)
{
nextReportID = (int)query.ExecuteScalar() + 1;
}
else
{
nextReportID = 1;
}
detailsAdapter.InsertCommand.Transaction = tran;
DataRow reportRow = ds.Tables["Reports"].NewRow();
reportRow["report_id"] = nextReportID;
DataRowView inspectorSelection = (DataRowView)inspectorBox.SelectedItem;
reportRow["inspector"] = Int16.Parse(inspectorSelection["empID"].ToString());
DataRowView empSelection = (DataRowView)employeeBox.SelectedItem;
reportRow["employee"] = Int16.Parse(inspectorSelection["empID"].ToString());
DataRowView locationSelection = (DataRowView)locationComboBox.SelectedItem;
reportRow["locationID"] = Int16.Parse(locationSelection["locID"].ToString());
reportRow["room"] = Int16.Parse(roomTextBox.Text);
reportRow["date"] = DateTime.Now.ToString("yyy-MM-dd");
reportRow["score"] = currentPoints;
ds.Tables["Reports"].Rows.Add(reportRow);
// update report_details dataset
foreach (DataRow row in ds.Tables["Grid"].Rows)
{
DataRow reportDetailsRow = ds.Tables["Details"].NewRow();
reportDetailsRow["reportID"] = nextReportID;
reportDetailsRow["itemID"] = row["ID"];
reportDetailsRow["points"] = row["Current"];
reportDetailsRow["comments"] = row["Comments"];
ds.Tables["Details"].Rows.Add(reportDetailsRow);
}
// update tables as single transaction
try
{
reportAdapter.Update(ds, "Reports");
detailsAdapter.Update(ds, "Details");
tran.Commit();
MessageBox.Show("Data Inserted");
}
catch (SqlException sqlEr)
{
MessageBox.Show(sqlEr.Message);
tran.Rollback();
}
}
我通过Microsoft( https://msdn.microsoft.com/zh-cn/library/33y2221y ( v=vs.110 ) .aspx )引用了这篇文章,但是据我了解,“订购”部分实际上是一个表时适用的需要更新。
谢谢!
首先用插入件满足外键容器的要求。 保留该值并使用外键关系执行第二次插入。 将这些插入内容包装在事务中。
begin transaction
INSERT INTO TableA (Id) VALUES (1)
INSERT INTO TableB (Id, TableAID) VALUES (newid(), 1)
commit transaction
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.