我正在开发一个应用程序,当用户点击提交按钮时,该数据库会将数据插入两个独立但相关的表中。
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/en-us/library/33y2221y(v=vs.110).aspx),但根据我的理解,Ordering部分确实适用于需要更新的一个表。
谢谢!
答案 0 :(得分:0)
首先用插入符合外键constaint的要求。保留该值并使用外键关系执行第二次插入。在事务中包装这些插入。
begin transaction
INSERT INTO TableA (Id) VALUES (1)
INSERT INTO TableB (Id, TableAID) VALUES (newid(), 1)
commit transaction