Hello,
I have a WinForm 2.0 application that uses a standard DataGridView control to allow inserts, updates and deletes.
I am implementing the bulk insert via the SqlDataAdapter bulk insert functionality, but am getting a primary key constraint violation of my table I am trying to insert into.
Violation of PRIMARY KEY constraint 'PK_tblLIDCustomersAddresses'.
Cannot insert duplicate key in object 'tblLIDCustomersAddresses'
Here is the code that implements this:
I'm probably missing something simple, but if anyone has a solution, please reply back.
Thanks in advance!
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);
}