0

我试图用数据集更新我的数据库表。我有一个可以编辑并导入数据集的本地 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”中插入重复键。

我在想它认为数据集表行是新行并且必须插入,但实际上不是。这些行存在于数据库中,并且只有其中一些被更新。

有人知道该怎么做吗?

4

1 回答 1

0

好吧,我自己发现了如何做到这一点。这就是我的方法的结果。这很完美。它仅更新文件 Dataset 和数据库中的 Dataset 中存在且具有不同数据的行。

我的方法:

 public void UpdateDatabase(DataSet data, string custID)
        {
            DataTable fromdatabase = new DataTable();
            DataTable fromFile = data.Tables[0];

            string connectionString = ConfigurationManager.ConnectionStrings["TestDbOnBrie"].ConnectionString;
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    string sqlCmd = "SELECT * FROM dbo.TransportSchedule_Customer WHERE CustID = '" + custID + "';";
                    SqlCommand command = new SqlCommand(sqlCmd, connection);
                    using (SqlCommand updateCmd = new SqlCommand("UPDATE dbo.TransportSchedule_Customer SET DeliveryDays1=@DeliveryDays1, DeliveryHours1=@DeliveryHours1, DeliveryType1=@DeliveryType1, DeliveryDays2=@DeliveryDays2, DeliveryHours2=@DeliveryHours2, DeliveryType2=@DeliveryType2, DeliveryDays3=@DeliveryDays3, DeliveryHours3=@DeliveryHours3, DeliveryType3=@DeliveryType3,  DistanceToDealer=@DistanceToDealer WHERE Alias=@Alias AND CustID=@CustID", connection))
                    {
                        using (SqlDataAdapter adapter = new SqlDataAdapter(command))
                        {
                            adapter.UpdateCommand = updateCmd;
                            adapter.Fill(fromdatabase);
                            updateCmd.Parameters.Add("@CustID", SqlDbType.VarChar, 50, "CustID");
                            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");

                            for (int i = 0; i < fromdatabase.Rows.Count; i++)
                            {
                                int Resultcompare = 0;
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryDays1"].ToString(), fromFile.Rows[i]["DeliveryDays1"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryHours1"].ToString(), fromFile.Rows[i]["DeliveryHours1"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryType1"].ToString(), fromFile.Rows[i]["DeliveryType1"].ToString());

                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryDays2"].ToString(), fromFile.Rows[i]["DeliveryDays2"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryHours2"].ToString(), fromFile.Rows[i]["DeliveryHours2"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryType2"].ToString(), fromFile.Rows[i]["DeliveryType2"].ToString());

                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryDays3"].ToString(), fromFile.Rows[i]["DeliveryDays3"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryHours3"].ToString(), fromFile.Rows[i]["DeliveryHours3"].ToString());
                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DeliveryType3"].ToString(), fromFile.Rows[i]["DeliveryType3"].ToString());


                                Resultcompare += string.Compare(fromdatabase.Rows[i]["DistanceToDealer"].ToString(), fromFile.Rows[i]["DistanceToDealer"].ToString());
                                if (Resultcompare == 0)
                                {

                                    //do nothing
                                }
                                else
                                {

                                    fromdatabase.Rows[i]["DeliveryDays1"] = fromFile.Rows[i]["DeliveryDays1"];
                                    fromdatabase.Rows[i]["DeliveryHours1"] = fromFile.Rows[i]["DeliveryHours1"];
                                    fromdatabase.Rows[i]["DeliveryType1"] = fromFile.Rows[i]["DeliveryType1"];

                                    fromdatabase.Rows[i]["DeliveryDays2"] = fromFile.Rows[i]["DeliveryDays2"];
                                    fromdatabase.Rows[i]["DeliveryHours2"] = fromFile.Rows[i]["DeliveryHours2"];
                                    fromdatabase.Rows[i]["DeliveryType2"] = fromFile.Rows[i]["DeliveryType2"];

                                    fromdatabase.Rows[i]["DeliveryDays3"] = fromFile.Rows[i]["DeliveryDays3"];
                                    fromdatabase.Rows[i]["DeliveryHours3"] = fromFile.Rows[i]["DeliveryHours3"];
                                    fromdatabase.Rows[i]["DeliveryType3"] = fromFile.Rows[i]["DeliveryType3"];

                                    fromdatabase.Rows[i]["DistanceToDealer"] = fromFile.Rows[i]["DistanceToDealer"];
                                    adapter.Update(fromdatabase);
                                }

                            }

                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine("ERROR in UpdateDatabase() method. Error Message : " + exception.Message);
                }
                finally
                {
                    if (connection.State == System.Data.ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
        }

如果您需要帮助,请发表评论。

于 2013-01-11T11:36:19.450 回答