我试图用数据集更新我的数据库表。我有一个可以编辑并导入数据集的本地 excel 文件。然后我想使用这个数据集和更新的数据来更新我的数据库表。这是我的代码:
public void UpdateDatabase(DataSet data, string tableName)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestDbOnBrie"].ConnectionString;
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM dbo.TransportSchedule_Customer;", connection))
{
SqlCommand selectCmd = new SqlCommand("SELECT * FROM dbo.TransportSchedule_Customer", connection);
adapter.SelectCommand = selectCmd;
SqlCommand updateCmd = new SqlCommand("UPDATE dbo.TransportSchedule_Customer SET Alias=@Alias, DeliveryDays1=@DeliveryDays1, DeliveryHours1=@DeliveryHours1, DeliveryType1=@DeliveryType1, DeliveryDays2=@DeliveryDays2, DeliveryHours2=@DeliveryHours2, DeliveryType2=@DeliveryType2, DeliveryDays3=@DeliveryDays3, DeliveryHours3=@DeliveryHours3, DeliveryType3=@DeliveryType3, DistanceToDealer=@DistanceToDealer WHERE AdrID=@AdrID AND CustID=@CustID", connection);
SqlCommand insertCmd = new SqlCommand("INSERT INTO dbo.TransportSchedule_Customer (CustID, Alias, AdrID, DeliveryDays1, DeliveryHours1, DeliveryType1, DeliveryDays2, DeliveryHours2, DeliveryType2, DeliveryDays3, DeliveryHours3, DeliveryType3, DistanceToDealer)" +
"VALUES (@CustID, @Alias, @AdrID, @DeliveryDays1, @DeliveryHours1, @DeliveryType1, @DeliveryDays2, @DeliveryHours2, @DeliveryType2, @DeliveryDays3, @DeliveryHours3, @DeliveryType3, @DistanceToDealer)", connection);
//insertCmd.Parameters.Add("@CustID", SqlDbType.VarChar, 50, "CustID");
//insertCmd.Parameters.Add("@AdrID", SqlDbType.Int, 50, "AdrID");
//insertCmd.Parameters.Add("@Alias", SqlDbType.VarChar, 50, "Alias");
//insertCmd.Parameters.Add("@DeliveryDays1", SqlDbType.VarChar, 50, "DeliveryDays1");
//insertCmd.Parameters.Add("@DeliveryHours1", SqlDbType.VarChar, 50, "DeliveryHours1");
//insertCmd.Parameters.Add("@DeliveryType1", SqlDbType.VarChar, 50, "DeliveryType1");
//insertCmd.Parameters.Add("@DeliveryDays2", SqlDbType.VarChar, 50, "DeliveryDays2");
//insertCmd.Parameters.Add("@DeliveryHours2", SqlDbType.VarChar, 50, "DeliveryHours2");
//insertCmd.Parameters.Add("@DeliveryType2", SqlDbType.VarChar, 50, "DeliveryType2");
//insertCmd.Parameters.Add("@DeliveryDays3", SqlDbType.VarChar, 50, "DeliveryDays3");
//insertCmd.Parameters.Add("@DeliveryHours3", SqlDbType.VarChar, 50, "DeliveryHours3");
//insertCmd.Parameters.Add("@DeliveryType3", SqlDbType.VarChar, 50, "DeliveryType3");
//insertCmd.Parameters.Add("@DistanceToDealer", SqlDbType.VarChar, 50, "DistanceToDealer");
//adapter.InsertCommand = insertCmd;
updateCmd.Parameters.Add("@CustID", SqlDbType.VarChar, 50, "CustID");
updateCmd.Parameters.Add("@AdrID", SqlDbType.Int, 50, "AdrID");
updateCmd.Parameters.Add("@Alias", SqlDbType.VarChar, 50, "Alias");
updateCmd.Parameters.Add("@DeliveryDays1", SqlDbType.VarChar, 50, "DeliveryDays1");
updateCmd.Parameters.Add("@DeliveryHours1", SqlDbType.VarChar, 50, "DeliveryHours1");
updateCmd.Parameters.Add("@DeliveryType1", SqlDbType.VarChar, 50, "DeliveryType1");
updateCmd.Parameters.Add("@DeliveryDays2", SqlDbType.VarChar, 50, "DeliveryDays2");
updateCmd.Parameters.Add("@DeliveryHours2", SqlDbType.VarChar, 50, "DeliveryHours2");
updateCmd.Parameters.Add("@DeliveryType2", SqlDbType.VarChar, 50, "DeliveryType2");
updateCmd.Parameters.Add("@DeliveryDays3", SqlDbType.VarChar, 50, "DeliveryDays3");
updateCmd.Parameters.Add("@DeliveryHours3", SqlDbType.VarChar, 50, "DeliveryHours3");
updateCmd.Parameters.Add("@DeliveryType3", SqlDbType.VarChar, 50, "DeliveryType3");
updateCmd.Parameters.Add("@DistanceToDealer", SqlDbType.VarChar, 50, "DistanceToDealer");
adapter.UpdateCommand = updateCmd;
adapter.Update(data, "1");
}
}
catch (Exception exception)
{
Console.WriteLine("ERROR in UpdateDatabase() method. Error Message : " + exception.Message);
}
finally
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
}
}
我的问题是,当我只使用更新命令时,我得到了这个错误:
当传递带有新行的 DataRow 集合时,更新需要有效的 InsertCommand。
当我同时使用更新命令和插入命令时,我收到此错误:
违反主键约束“PK_TransportSchedule_Customer”。无法在对象“dbo.TransportSchedule_Customer”中插入重复键。
我在想它认为数据集表行是新行并且必须插入,但实际上不是。这些行存在于数据库中,并且只有其中一些被更新。
有人知道该怎么做吗?