如何在不使用循环的情况下更新表中的多行?

时间:2016-09-13 02:48:18

标签: c#

我确信必须有办法做到这一点,因为如果你不能一次改变多行,ExecuteNonQuery将不存在,但我无法弄清楚如何。目前,我正在使用以下代码更新Customer表中的值:

            for (int i = 0; i < dataGridView1.RowCount; i++)
        {

            string connectionString = @"Data Source = A103-17\SQLEXPRESS17; Initial Catalog = CarRental; Integrated Security = True";

            string myUpdate = "UPDATE [CarRental].[dbo].[Customer] " +
//                "set [CustomerID] = @CustomerID, " +
            "set [FirstName] = @FirstName, " +
            "[LastName] = @LastName, " +
            "[StreetNo] = @StreetNo, " +
            "[StreetName]= @StreetName, " +
            "[Suburb] = @Suburb, " +
            "[State] = @State, " +
            "[PostCode] = @PostCode, " +
            "[MobPhone] = @MobPhone, " +
            "[DriverLicNo] = @DriverLicNo, " +
            "[CreditCardType] = @CreditCardType, " +
            "[CreditCardNo] = @CreditCardNo, " +
            "[ExpDate] = @ExpDate, " +
            "[NameOnCreditCard] = @NameOnCreditCard " +
            "WHERE [CustomerID] = @CustomerID" +
            ";";

            SqlConnection con = new SqlConnection(connectionString);
            using (SqlCommand ApptUpdate = new SqlCommand(myUpdate, con))
            {
                ApptUpdate.Parameters.AddWithValue("@CustomerID", dataGridView1.Rows[i].Cells["CustomerID"].Value ?? DBNull.Value);
                ApptUpdate.Parameters.AddWithValue("@FirstName", dataGridView1.Rows[i].Cells["FirstName"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@LastName", dataGridView1.Rows[i].Cells["LastName"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@StreetNo", dataGridView1.Rows[i].Cells["StreetNo"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@StreetName", dataGridView1.Rows[i].Cells["StreetName"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@Suburb", dataGridView1.Rows[i].Cells["Suburb"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@State", dataGridView1.Rows[i].Cells["State"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@PostCode", dataGridView1.Rows[i].Cells["PostCode"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@MobPhone", dataGridView1.Rows[i].Cells["MobPhone"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@DriverLicNo", dataGridView1.Rows[i].Cells["DriverLicNo"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@CreditCardType", dataGridView1.Rows[i].Cells["CreditCardType"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@CreditCardNo", dataGridView1.Rows[i].Cells["CreditCardNo"].Value ?? "Null");
                ApptUpdate.Parameters.AddWithValue("@ExpDate", dataGridView1.Rows[i].Cells["ExpDate"].Value ?? DateTime.Now);
                ApptUpdate.Parameters.AddWithValue("@NameOnCreditCard", dataGridView1.Rows[i].Cells["NameOnCreditCard"].Value ?? "Null");

                con.Open();
                MessageBox.Show(ApptUpdate.ExecuteNonQuery().ToString());
                con.Close();
            };
        }

    }

这样可行,但会弹出消息框,显示每行的值为1,而不是一次显示与更改行数相对应的值。我无法将执行移到循环之外,所以我无法弄清楚如何在ExecuteNonQuery返回的行中获得任何用途。如何才能正确使用该功能?

1 个答案:

答案 0 :(得分:2)

首先将所有CustomerID组合成逗号分隔的字符串。 然后使用$state.go(stateC)

代替where CustomerID = @CustomerID

where CustomerID in (@CustomerIds)是你的字符串