使用数据集问题更新数据库表

时间:2013-01-09 09:58:01

标签: c# .net dataset sqlcommand sqldataadapter

我试图用数据集更新我的数据库表。我有一个本地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。

当我同时使用update命令和Insert命令时,我收到此错误:

违反PRIMARY KEY约束'PK_TransportSchedule_Customer'。无法在对象'dbo.TransportSchedule_Customer'中插入重复键。

我认为它认为数据集表行是新行并且必须插入,但实际上并非如此。行存在数据库,只有一些更新。

doe的任何人都有任何关于该做什么的想法?

1 个答案:

答案 0 :(得分:0)

我自己发现了如何做到这一点。这是我的方法结果。这很完美。它只更新存在的行,并在数据集和数据集中包含数据集中的不同数据。

我的方法:

 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();
                    }
                }
            }
        }

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