У меня проблема с моим кодом, когда несколько пользователей работают одновременно. что он показывает мне ошибку:
«Идентификатор столбца» должен быть уникальным. Значение «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 объясняется здесь, и я сделал так, как они объяснили. как получить значение идентификатора < /а>