C# 以另一种形式从选定行更新数据库

C# 以另一种形式从选定行更新数据库,c#,database,C#,Database,我在更新datagridview上的选定行时遇到问题,该行以另一种形式从数据库中提取。 我使用它将信息从datagridview获取到另一个表单的文本框中: private void updateAppointmentButton_Click(object sender, EventArgs e) { UpdateAppointment updateAppointment = new UpdateAppointment(); upd

我在更新datagridview上的选定行时遇到问题,该行以另一种形式从数据库中提取。 我使用它将信息从datagridview获取到另一个表单的文本框中:

    private void updateAppointmentButton_Click(object sender, EventArgs e)
    {
            UpdateAppointment updateAppointment = new UpdateAppointment();
            updateAppointment.mainFormObject = this;
            updateAppointment.customerIdBox.Text = appointmentCalendar.CurrentRow.Cells[0].Value.ToString();
            updateAppointment.customerNameBox.Text = appointmentCalendar.CurrentRow.Cells[1].Value.ToString();
            updateAppointment.typeBox.Text = appointmentCalendar.CurrentRow.Cells[2].Value.ToString();
            updateAppointment.startTimeBox.Value = Convert.ToDateTime(appointmentCalendar.CurrentRow.Cells[4].Value.ToString());
            updateAppointment.endTimeBox.Value = Convert.ToDateTime(appointmentCalendar.CurrentRow.Cells[3].Value.ToString());

            updateAppointment.Show();



        MySqlConnection c = new MySqlConnection(SqlUpdater.conString);
        MySqlCommand updateCmd = new MySqlCommand();

        updateCmd.Connection = c;
        c.Open();
        updateCmd.CommandText = $"UPDATE customer SET customerName = '{customerNameBox.Text}'";
        updateCmd.ExecuteNonQuery();
        c.Close();
        MessageBox.Show("Appointment Updated");
我认为这是SQL查询的问题,但不确定如何将其仅限于所选行上的信息。现在,它将更新datagridview和数据库中的所有人

有什么想法吗? 谢谢

我试过把 MainForm.appointmentCalendar.CurrentRow.Cells[1].Value.ToString()


与SQL查询中的WHERE相同,但它返回一个“需要对象引用”错误。

有一个具有唯一客户ID的列,然后在您的查询中

Update customer SET customerName = '{customerNameBox.Text}' where customerID = 'UniqueID'
-----(无论您尝试更新的ID是什么)

可能类似于
int.Parse(otherDataGrid.selectedRows[0].Cells[“ID”].Value.ToString())

@编辑

我真的不明白你想说什么。您可能需要尝试使用参数。这将是您的问题:

Update appointment set type = @type, start = @start, end = @end where customerId = @id
然后在执行命令之前,请说:

updateCmd.Parameters.AddWithValue("@type", typeBox.Text);
对所有其他参数也这样做。
还要确保您的文本框不是空的,因为如果您的查询正在删除数据(可能是用空字符串更新数据),它们很可能是空的

Update customer SET customerName = '{customerNameBox.Text}' where customerID = 'UniqueID'
-----(无论您尝试更新的ID是什么)

可能类似于
int.Parse(otherDataGrid.selectedRows[0].Cells[“ID”].Value.ToString())

@编辑

我真的不明白你想说什么。您可能需要尝试使用参数。这将是您的问题:

Update appointment set type = @type, start = @start, end = @end where customerId = @id
然后在执行命令之前,请说:

updateCmd.Parameters.AddWithValue("@type", typeBox.Text);
对所有其他参数也这样做。
还要确保您的文本框不是空的,因为如果您的查询正在删除数据(可能是用空字符串更新数据),它们很可能是空的。

请参阅下面的帖子中的“我的两个表单项目”:piotrb92:应该可以,而不是更新人员的ID和姓名,只需更新时间…让我试一试。请参阅下面帖子中的我的两个表单项目:piotrb92:这应该可以工作,而不是能够更新此人的ID和姓名,只需更新时间…让我试一试。确定运行时使用:WHERE customerId='{customerIdBox.Text}'我确实将查询更改为:UPDATE约会集type='{typeBox.Text}',start='{startTimeBox.Value.ToString()}',end='{endTimeBox.Value.ToString()}',其中customerId='{customerIdBox.Text}',现在,它删除开始/结束时间,并将类型设置为所有记录上相同的位置(具有相同类型)…看看我编辑的答案,我无法使用参数将其发布为评论,但我认为我的部分问题在于数据库本身。“type column”下有2个条目,因此如果我更新它,那么它将为每个人更改它。确定运行时使用:WHERE customerId='{customerIdBox.Text}'我确实将查询更改为:UPDATE appointment SET type='{typeBox.Text}',start='{startTimeBox.Value.ToString()}',end='{endTimeBox.Value.ToString()}'WHERE customerId='{customerIdBox.Text}'现在,它删除了开始/结束时间,并将类型设置为相同的内容,在所有记录上(使用相同的类型)…看看我编辑的答案,我无法使用参数将其作为注释发布,但我认为部分问题在于数据库本身。“类型列”下有2个条目,因此如果我要更新它,那么它将为每个人更改它。