我有以下代码:
using (var connection = new SqlConnection(connetionString))
{
try
{
connection.Open();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("DatabasConnection Done");
DateTime datum = DateTime.Now;
string LastChangedBy = "System";
foreach (Person p in myPersons)
{
SqlCommand command1 = new SqlCommand();
try
{
command1 = Avreg(p.UnregistrationReason, p.GivenNameNumber, p.ProtectedIdentity, p.CitizenshipDate, connection);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("LALALALA Done");
command1.Parameters.AddWithValue("@PersonalIdentityNumber", string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
command1.Parameters.AddWithValue("@FirstName", p.FirstName ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@LastName", p.LastName ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationCountyCode", p.NationalRegistrationCountyCode ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationMunicipalityCode", p.NationalRegistrationMunicipalityCode ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress1", p.NationalRegistrationDistributionAddress1 ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationDistributionAddress2", p.NationalRegistrationDistributionAddress2 ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationPostCode", p.NationalRegistrationPostCode ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationCity", p.NationalRegistrationCity ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@BirthCountyCode", p.BirthCountyCode ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@UnregistrationDate", p.UnregistrationDate ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@BirthParish", p.BirthParish ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@CitizenshipCode", p.CitizenshipCode ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@CitizenshipDate", p.CitizenshipDate ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@NationalRegistrationDate", p.NationalRegistrationDate ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@ForeignDistrubtionAddress1", p.ForeignDistrubtionAddress1 ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@ForeignDistrubtionAddress2", p.ForeignDistrubtionAddress2 ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@ForeignDistrubtionAddress3", p.ForeignDistrubtionAddress3 ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@ForeignBirthCity", p.ForeignBirthCity ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@LastChangedBy", LastChangedBy);
//command1.Parameters.AddWithValue("@LastChangedDate", datum);
command1.ExecuteNonQuery();
Console.WriteLine(string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
}
}
Console.WriteLine("Done");
Console.WriteLine("Alla fält uppdaterade");
Console.ReadKey();
}// Put a break-point here, then mouse-over PersonalIdentityNumber... deserializedList contains everything if you need it
catch (Exception e)
{
Console.Write(" ---- FEL VID INLÄSNINGEN ------ " + e.Message);
Console.ReadKey();
}
}
public static SqlCommand Avreg(string s, string t, string p, string c, SqlConnection connection)
{
try
{
var query = "UPDATE Seamen SET FirstName = @FirstName";//, "+
//"LastName = @LastName, "+
//"NationalRegistrationCountyCode = @NationalRegistrationCountyCode, "+
//"NationalRegistrationMunicipalityCode = @NationalRegistrationMunicipalityCode, "+
//"NationalRegistrationDistributionAddress1 = @NationalRegistrationDistributionAddress1, "+
//"NationalRegistrationDistributionAddress2 = @NationalRegistrationDistributionAddress2, "+
//"UnregistrationDate = @UnregistrationDate, "+
//"NationalRegistrationPostCode = @NationalRegistrationPostCode, "+
//"NationalRegistrationCity = @NationalRegistrationCity, "+
//"BirthCountyCode = @BirthCountyCode, "+
//"BirthParish = @BirthParish, "+
//"CitizenshipCode = @CitizenshipCode, "+
//"CitizenshipDate = @CitizenshipDate, " +
//"NationalRegistrationDate = @NationalRegistrationDate, "+
//"ForeignDistrubtionAddress1 = @ForeignDistrubtionAddress1, "+
//"ForeignDistrubtionAddress2 = @ForeignDistrubtionAddress2, "+
//"ForeignDistrubtionAddress3 = @ForeignDistrubtionAddress3, "+
//"ForeignBirthCity = @ForeignBirthCity, "+
//"LastChangedBy = @LastChangedBy, "+
//"LastChangedDate = @LastChangedDate";
SqlCommand command1;
if (c == "0")
{
query += ", CitizenshipDate = null";
command1 = new SqlCommand(query, connection);
}
else
{
query += ", CitizenshipDate = @CitizenshipDate";
command1 = new SqlCommand(query, connection);
command1.Parameters.AddWithValue("@CitizenshipDate", c ?? DBNull.Value.ToString());
}
if (p == "J")
{
query = "UPDATE Seamen SET FirstName ='Skyddad personuppgift'";//, " +
//"LastName = 'Se hjälptext', " +
//"ProtectedIdentity = '1', " +
//"NationalRegistrationCountyCode = NULL, " +
//"NationalRegistrationMunicipalityCode = NULL, " +
//"NationalRegistrationCoAddress = NULL, " +
////"NationalRegistrationDistributionAddress1 = NULL, " +
////"NationalRegistrationDistributionAddress2 = NULL, " +
//"UnregistrationDate = NULL, " +
//"NationalRegistrationPostCode = NULL, " +
//"NationalRegistrationCity = NULL, " +
//"BirthCountyCode = NULL, " +
//"BirthParish = NULL, " +
//"CitizenshipCode = NULL, " +
// //"CitizenshipDate = @CitizenshipDate, " +
//"NationalRegistrationDate = NULL, " +
//"ForeignDistrubtionAddress1 = NULL, " +
//"ForeignDistrubtionAddress2 = NULL, " +
//"ForeignDistrubtionAddress3 = NULL, " +
//"UnregistrationReason = NULL, " +
//"ForeignBirthCity = NULL, " +
//"LastChangedBy = @LastChangedBy, " +
//"GivenNameNumber = NULL, " +
//"LastChangedDate = @LastChangedDate WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
command1 = new SqlCommand(query, connection);
command1.Parameters.Clear();
return command1;
}
if ((!string.IsNullOrEmpty(s)) && !string.IsNullOrEmpty(t))
{
query += ", UnregistrationReason = @UnregistrationReason";
query += ", GivenNameNumber = @GivenNameNumber ";
command1 = new SqlCommand(query, connection);
command1.Parameters.AddWithValue("@UnregistrationReason", s ?? DBNull.Value.ToString());
command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());
}
else if (!string.IsNullOrEmpty(s) && string.IsNullOrEmpty(t))
{
query += ", UnregistrationReason = @UnregistrationReason, GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
command1 = new SqlCommand(query, connection);
t = "00";
command1.Parameters.AddWithValue("@UnregistrationReason", s ?? DBNull.Value.ToString());
command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());
}
else if (string.IsNullOrEmpty(s) && !string.IsNullOrEmpty(t))
{
query += ", GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
command1 = new SqlCommand(query, connection);
command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());
}
else
{
query += ", GivenNameNumber = @GivenNameNumber WHERE PersonalIdentityNumber = @PersonalIdentityNumber";
t = "00";
command1 = new SqlCommand(query, connection);
command1.Parameters.AddWithValue("@GivenNameNumber", t ?? DBNull.Value.ToString());
return command1;
}
return command1;
}
catch(Exception e)
{
throw;
}
}
//}
此处发生错误:
command1.Parameters.AddWithValue("@PersonalIdentityNumber", string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
command1.Parameters.AddWithValue("@FirstName", p.FirstName ?? DBNull.Value.ToString());
//command1.Parameters.AddWithValue("@LastName", p.LastName ?? DBNull.Value.ToString());
command1.ExecuteNonQuery();
Console.WriteLine(string.Format("{0}{1}", p.PersonalIdentityNumber, p.SpecialIdentityNumber));
我收到错误:
违反主键约束' PK_logSeamen'。无法插入 对象' dbo.logSeamen'中的重复键。声明一直如此 终止。
我是否需要清除SqlCommand
?
显然我有这个触发器
USE [Bums]
GO
/****** Object: Trigger [dbo].[trSeamen_LogI] Script Date: 05/16/2016 11:43:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trSeamen_LogI] ON [dbo].[Seamen] AFTER INSERT AS
INSERT INTO [logSeamen] ([PersonalIdentityNumber], [ProtectedIdentity], [ReferedCivicRegistrationNumber], [UnregistrationReason], [UnregistrationDate], [MessageComputerComputer], [GivenNameNumber], [FirstName], [MiddleName], [LastName], [NotifyName], [NationalRegistrationDate], [NationalRegistrationCountyCode], [NationalRegistrationMunicipalityCode], [NationalRegistrationCoAddress], [NationalRegistrationDistributionAddress1], [NationalRegistrationDistributionAddress2], [NationalRegistrationPostCode], [NationalRegistrationCity], [NationalRegistrationNotifyDistributionAddress], [NationalRegistrationNotifyPostCode], [NationalRegistrationNotifyCity], [ForeignDistrubtionAddress1], [ForeignDistrubtionAddress2], [ForeignDistrubtionAddress3], [ForeignDistrubtionCountry], [ForeignDate], [BirthCountyCode], [BirthParish], [ForeignBirthCity], [CitizenshipCode], [CitizenshipDate], [Email], [Telephone], [Mobiletelephone], [Gender], [NotNewsPaper], [Note], [StatusCode], [NationalRegistrationCode], [RegistrationDate], [LastUpdatedFromNavet], [TemporaryDistrubtionAddress1], [TemporaryDistrubtionAddress2], [TemporaryDistrubtionAddress3], [TemporaryDistrubtionCountry], [Password], [VisibilityLevel],[SeamanIdentity], [LastChangedBy], [LastChangedDate], LogAction)
SELECT [PersonalIdentityNumber], [ProtectedIdentity], [ReferedCivicRegistrationNumber], [UnregistrationReason], [UnregistrationDate], [MessageComputerComputer], [GivenNameNumber], [FirstName], [MiddleName], [LastName], [NotifyName], [NationalRegistrationDate], [NationalRegistrationCountyCode], [NationalRegistrationMunicipalityCode], [NationalRegistrationCoAddress], [NationalRegistrationDistributionAddress1], [NationalRegistrationDistributionAddress2], [NationalRegistrationPostCode], [NationalRegistrationCity], [NationalRegistrationNotifyDistributionAddress], [NationalRegistrationNotifyPostCode], [NationalRegistrationNotifyCity], [ForeignDistrubtionAddress1], [ForeignDistrubtionAddress2], [ForeignDistrubtionAddress3], [ForeignDistrubtionCountry], [ForeignDate], [BirthCountyCode], [BirthParish], [ForeignBirthCity], [CitizenshipCode], [CitizenshipDate], [Email], [Telephone], [Mobiletelephone], [Gender], [NotNewsPaper], [Note], [StatusCode], [NationalRegistrationCode], [RegistrationDate], [LastUpdatedFromNavet], [TemporaryDistrubtionAddress1], [TemporaryDistrubtionAddress2], [TemporaryDistrubtionAddress3], [TemporaryDistrubtionCountry], [Password], [VisibilityLevel], [SeamanIdentity], [LastChangedBy], [LastChangedDate], 'INSERT' FROM INSERTED
如何确保它不违反主键约束?
更新2
USE [Bums]
GO
/****** Object: Trigger [dbo].[trSeamen_LogU] Script Date: 05/16/2016 12:53:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[trSeamen_LogU] ON [dbo].[Seamen] AFTER UPDATE AS
INSERT INTO [logSeamen] ([PersonalIdentityNumber], [ProtectedIdentity], [ReferedCivicRegistrationNumber], [UnregistrationReason], [UnregistrationDate], [MessageComputerComputer], [GivenNameNumber], [FirstName], [MiddleName], [LastName], [NotifyName], [NationalRegistrationDate], [NationalRegistrationCountyCode], [NationalRegistrationMunicipalityCode], [NationalRegistrationCoAddress], [NationalRegistrationDistributionAddress1], [NationalRegistrationDistributionAddress2], [NationalRegistrationPostCode], [NationalRegistrationCity], [NationalRegistrationNotifyDistributionAddress], [NationalRegistrationNotifyPostCode], [NationalRegistrationNotifyCity], [ForeignDistrubtionAddress1], [ForeignDistrubtionAddress2], [ForeignDistrubtionAddress3], [ForeignDistrubtionCountry], [ForeignDate], [BirthCountyCode], [BirthParish], [ForeignBirthCity], [CitizenshipCode], [CitizenshipDate], [Email], [Telephone], [Mobiletelephone], [Gender], [NotNewsPaper], [Note], [StatusCode], [NationalRegistrationCode], [RegistrationDate], [LastUpdatedFromNavet], [TemporaryDistrubtionAddress1], [TemporaryDistrubtionAddress2], [TemporaryDistrubtionAddress3], [TemporaryDistrubtionCountry], [Password], [VisibilityLevel], [SeamanIdentity],[LastChangedBy], [LastChangedDate], LogAction)
SELECT [PersonalIdentityNumber], [ProtectedIdentity], [ReferedCivicRegistrationNumber], [UnregistrationReason], [UnregistrationDate], [MessageComputerComputer], [GivenNameNumber], [FirstName], [MiddleName], [LastName], [NotifyName], [NationalRegistrationDate], [NationalRegistrationCountyCode], [NationalRegistrationMunicipalityCode], [NationalRegistrationCoAddress], [NationalRegistrationDistributionAddress1], [NationalRegistrationDistributionAddress2], [NationalRegistrationPostCode], [NationalRegistrationCity], [NationalRegistrationNotifyDistributionAddress], [NationalRegistrationNotifyPostCode], [NationalRegistrationNotifyCity], [ForeignDistrubtionAddress1], [ForeignDistrubtionAddress2], [ForeignDistrubtionAddress3], [ForeignDistrubtionCountry], [ForeignDate], [BirthCountyCode], [BirthParish], [ForeignBirthCity], [CitizenshipCode], [CitizenshipDate], [Email], [Telephone], [Mobiletelephone], [Gender], [NotNewsPaper], [Note], [StatusCode], [NationalRegistrationCode], [RegistrationDate], [LastUpdatedFromNavet], [TemporaryDistrubtionAddress1], [TemporaryDistrubtionAddress2], [TemporaryDistrubtionAddress3], [TemporaryDistrubtionCountry], [Password], [VisibilityLevel], [SeamanIdentity],[LastChangedBy], [LastChangedDate], 'UPDATE' FROM INSERTED
答案 0 :(得分:1)
RE
违反主键约束'PK_logSeamen'。无法插入 对象'dbo.logSeamen'中的重复键。声明一直如此 终止。
看起来你在Seamen表上有一个更新触发器。检查您的触发器正在做什么,并在必要时进行修复。
答案 1 :(得分:1)
您的触发器是从Inserted表插入一系列值,您说的是PersonalIdentityNumber是LogSeamen中的主键。然后很明显,每次更新都会违反它,因为它已经在第一次插入操作时插入了。删除PK_LogSeamen,或者更好的是为PK创建另一个自动生成的列。
答案 2 :(得分:0)
更新可能会在桌面上多次发生。您的logSeamen
表用于跟踪更改,并且更新的行总是可能在过去的某个时间更新过。您无法将PersonalIdentityNumber
(或Seamen
表中的任何列)标记为logSeamen
表中的主键。
要解决问题,
从PersonalIdentityNumber
表格中的logSeamen
列中删除主键约束,以便您可以多次插入相同的PersonalIdentityNumber
。
或者,添加一个新列(例如logSeamenId
)作为INDENTITY(1,1)
的主键,这将为该行自动生成一个数字。