【发布时间】:2020-04-19 16:36:13
【问题描述】:
当多个用户同时工作时,我的代码出现问题。它向我显示了一个错误:
'列'id'被限制为唯一。值“9”已经存在。'
我尝试在一个小代码上对其进行模拟,以便您了解问题所在。
假设我们有一个数据库,其中有一个名为 Table1 的表
CREATE TABLE table1 (
[id] int not null IDENTITY(1,1) PRIMARY KEY,
[Name] nvarchar(max) not null,
);
我有这个简单的控制台应用程序:
static void Main(string[] args)
{
const string connectionString = "Server=.;Database=Test;User Id=sa;Password=;";
using var connection = new SqlConnection(connectionString);
connection.Open();
using var adapter = new SqlDataAdapter("Select * from [Table1]", connection)
{
InsertCommand = new SqlCommand("Insert into Table1 ([Name]) values(@NameParam); Select [ID],[Name] from [Table1] where [id]= SCOPE_IDENTITY();",connection)
{
CommandType = CommandType.Text
}
};
adapter.InsertCommand.Parameters.Add(new SqlParameter("@NameParam", SqlDbType.NVarChar, 40, "Name"));
adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
// MissingSchemaAction adds any missing schema to
// the DataTable, including identity columns
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
adapter.RowUpdated += Adapter_RowUpdated;
var table = new DataTable();
adapter.Fill(table);
// this insert command simulates another user on the network inserted a row
var anotherInsertCommand = new SqlCommand("Insert into Table1 ([Name]) values('Test1');",connection);
anotherInsertCommand.ExecuteNonQuery();
DataRow newRow = table.NewRow();
newRow["Name"] = "test2";
table.Rows.Add(newRow);
DataRow newRow1 = table.NewRow();
newRow1["Name"] = "test3";
table.Rows.Add(newRow1);
DataTable changedData = table.GetChanges();
adapter.Update(changedData);
table.Merge(changedData);
table.AcceptChanges();
}
private static void Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
{
// If this is an insert, then skip this row.
if (e.StatementType != StatementType.Insert) return;
if (e.Status == UpdateStatus.ErrorsOccurred)
{
// here it will show an error that the retrieved Identity column is already exist on the dataTable. Because we still didn't update the second row
throw e.Errors;
}
e.Status = UpdateStatus.SkipCurrentRow;
}
现在的问题是,我在说明应用程序时填写了我的数据表,因此数据表将没有行,只有架构。
稍后使用同一应用程序的另一个用户在数据库中插入一行。但我的 dataTable 不会知道这一行。
之后,我在 dataTable 上插入两行并尝试检索 Identity 列值。但它向我显示了这个错误。
有没有人有一个简单快速的方法来解决这个问题。比如禁用约束什么的。我不知道。
注意:这里解释了我检索 Identity 值的方式,我按照他们的解释做了。 how to retrieve Identity value
【问题讨论】:
-
我试图通过这个 changedData.Constraints.Clear(); 清除约束然后返回它们并且它起作用了。但这是正确的方法吗?我不知道我需要你的建议
标签: c# sql-server ado.net ado sqldataadapter