У меня есть приложение WinForm 2.0, которое использует стандартный элемент управления DataGridView, чтобы разрешать вставки, обновления и удаления.
Я реализую массовую вставку с помощью функции массовой вставки SqlDataAdapter, но получаю нарушение ограничения первичного ключа для моей таблицы, в которую пытаюсь вставить.
Нарушение ограничения ПЕРВИЧНЫЙ КЛЮЧ
'PK_tblLIDCustomersAddresses'.
Не может
вставить дубликат ключа в объект
'TblLIDCustomersAddresses'
Вот код, который реализует это:
Я, вероятно, упускаю что-то простое, но если у кого-то есть решение, пожалуйста, ответьте.
Заранее спасибо!
StringBuilder insertSQL = new StringBuilder();
insertSQL.Append("INSERT INTO tblLIDCustomersAddresses(");
insertSQL.Append("lngLIDCustomerID,strAddressType,strCustomerAddress,strCustomerLocation,");
insertSQL.Append("strCustomerStreetName,strCustomerSuite,strCustomerStreetTypes,");
insertSQL.Append("strCustomerCity,strCustomerState,strCustomerZip,dtmUpdated,strUserUpdated)");
insertSQL.Append("VALUES(@lngLIDCustomerID,@strAddressType,@strCustomerAddress,");
insertSQL.Append("@strCustomerLocation,@strCustomerStreetName,");
insertSQL.Append("@strCustomerSuite,@strCustomerStreetTypes,@strCustomerCity,");
insertSQL.Append("@strCustomerState,@strCustomerZip,@dtmUpdated,@strUserUpdated)");
insertSQL.AppendLine("SET @CustomerAddressID = SCOPE_IDENTITY()");
using (SqlConnection cn = Utilities.CreateSQLConnection(Properties.Settings.Default.DBConnection))
{
da = new SqlDataAdapter();
da.InsertCommand = new SqlCommand(insertSQL.ToString(), cn);
da.InsertCommand.Parameters.Add("@lngLIDCustomerID", SqlDbType.Int, 4, "lngLIDCustomerID");
da.InsertCommand.Parameters.Add("@strAddressType", SqlDbType.VarChar, 20, "strAddressType");
da.InsertCommand.Parameters.Add("@strCustomerAddress", SqlDbType.VarChar, 25, "strCustomerAddress");
da.InsertCommand.Parameters.Add("@strCustomerLocation", SqlDbType.VarChar, 2, "strCustomerLocation");
da.InsertCommand.Parameters.Add("@strCustomerStreetName", SqlDbType.VarChar, 50, "strCustomerStreetName");
da.InsertCommand.Parameters.Add("@strCustomerSuite", SqlDbType.VarChar, 10, "strCustomerSuite");
da.InsertCommand.Parameters.Add("@strCustomerStreetTypes", SqlDbType.VarChar, 4, "strCustomerStreetTypes");
da.InsertCommand.Parameters.Add("@strCustomerCity", SqlDbType.VarChar, 35, "strCustomerCity");
da.InsertCommand.Parameters.Add("@strCustomerState", SqlDbType.VarChar, 2, "strCustomerState");
da.InsertCommand.Parameters.Add("@strCustomerZip", SqlDbType.VarChar, 10, "strCustomerZip");
da.InsertCommand.Parameters.AddWithValue("@dtmUpdated", DateTime.Now.ToString());
da.InsertCommand.Parameters.AddWithValue("@strUserUpdated", this._user.Name);
da.InsertCommand.Parameters.Add("@CustomerAddressID", SqlDbType.Int, 4, "CustomerAddressID");
da.InsertCommand.Parameters["@CustomerAddressID"].Direction = ParameterDirection.Output;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
da.UpdateBatchSize = 0; // set batch size to maximum value possible
da.Update(addressChanges);
}