Reputation: 741
I have a database and I am trying to add some rows into it. These rows come from a datatable, the problem is that my SQL Server table has a primary key to autoincrement, I am not sure how to accomplish this so what I did was send always a 1 thinking the database would ignore and place correct value but didn't.
I got this error
Violation of PRIMARY KEY constraint 'PK_NWActivity'. Cannot insert duplicate key in object 'dbo.Activity'. The duplicate key value is (1).
My code is this
if (owner_response != null)
{
DataTable periodData = new DataTable();
periodData.Columns.Add("idActivity", typeof(int));
periodData.Columns.Add("eventSubType", typeof(String));
periodData.Columns.Add("antena", typeof(String));
periodData.Columns.Add("economico", typeof(String));
periodData.Columns.Add("Latitude", typeof(float));
periodData.Columns.Add("Longitud", typeof(float));
periodData.Columns.Add("Location", typeof(String));
periodData.Columns.Add("site", typeof(String));
periodData.Columns.Add("IgnitionOn", typeof(Boolean));
periodData.Columns.Add("speed", typeof(float));
periodData.Columns.Add("activityDate", typeof(DateTime));
periodData.Columns.Add("sensor1", typeof(Boolean));
periodData.Columns.Add("sensor2", typeof(Boolean));
foreach (var owner in owner_response.Owners)
{
var period_response = new GetPeriodActivityResponse();
do
{
period_response = proxy.GetActivityByOwner(owner.OwnerId,startTimer , (period_response.Version != 0) ? period_response.Version : 0);
if (period_response != null)
{
periodData.Clear();
foreach (var activity in period_response.Activities)
{
DataRow[] vehiculo = vehicleData.Select("[VehicleId] = '" + activity.VehicleID +"'");
DataRow[] sitio = SiteTable.Select("[SiteId] = '" + activity.SiteID + "'");
periodData.Rows.Add(
1,
activity.EventSubType,
vehiculo[0][15],
vehiculo[0][4],
activity.Latitude,
activity.Longitude,
activity.Location,
(sitio.Length > 1) ? sitio[4].ToString() : "",
activity.IgnitionOn,
activity.Speed,
activity.ActivityDateTime.ToLocalTime(),
false,
false);
}
}
} while (period_response.MoreItemsAvailable);
}
if (periodData.Rows.Count > 0)
{
SqlBulkCopy bc = new SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock);
bc.DestinationTableName = "Activity";
bc.BatchSize = periodData.Rows.Count;
if (con.State != ConnectionState.Open)
{
con.Close();
con.Open();
}
bc.WriteToServer(periodData);
bc.Close();
con.Close();
}
}
Upvotes: 2
Views: 1305
Reputation: 507
Just ignore the primarykey column. Don't add it to the DataTable, the server should take care of the value.
DataTable periodData = new DataTable();
//periodData.Columns.Add("idActivity", typeof(int));
periodData.Columns.Add("eventSubType", typeof(String));
periodData.Columns.Add("antena", typeof(String));
periodData.Columns.Add("economico", typeof(String));
periodData.Columns.Add("Latitude", typeof(float));
periodData.Columns.Add("Longitud", typeof(float));
periodData.Columns.Add("Location", typeof(String));
periodData.Columns.Add("site", typeof(String));
periodData.Columns.Add("IgnitionOn", typeof(Boolean));
periodData.Columns.Add("speed", typeof(float));
periodData.Columns.Add("activityDate", typeof(DateTime));
periodData.Columns.Add("sensor1", typeof(Boolean));
periodData.Columns.Add("sensor2", typeof(Boolean));
...
Upvotes: 2