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

帕特里克·戴维斯

我在更新以另一种形式从数据库中提取的 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,但它返回“需要对象引用”错误。

piotrb92

有一个具有唯一客户 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);

并对所有其他参数也这样做。还要确保您的文本框不为空,因为它们很可能是您的查询正在删除数据(也许它正在用空字符串更新它)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

数据库播种仅适用于一种模型,不适用于另一种模型

如何将DataGridView选定的行值以另一种形式传递给TextBox?

使用数据库查找从另一种类型的对象创建一种类型的对象

数据库查询retuyrns字段以一种顺序显示,我想以另一种显示它们

我过度查询MySql数据库。检索许多数据的另一种方法?

如何将数据库文件名通过一种形式传递给另一种形式

DAO使用一种方法更新数据库,但不使用另一种方法

是否有另一种方法来更改Autofac中的数据库实例

以另一种形式更新数据后刷新datagridview

从一种形式到另一种形式检索DataGridView中的选定行/单元格值

从另一种形式更新ListView

通过get使用javascript或jquery将数据从一种形式推送到另一种形式

从数据库将字符串从一种形式发送到另一种形式

当我以另一种形式编辑了一行值时,我的dataGridView没有更新

在Windows Form C#中将数据从一种形式传递到另一种形式

在Firebase中将JSON文件导入实时数据库的另一种方法

如何执行从一种形式到另一种形式的更新查询

从数据库获取数据的另一种方法

如何从另一种形式访问数据网格

将 JSON 数组数据的一种形式转换为另一种形式?

如何从另一种形式的按钮更新 DataGridView?

在 Yii2 中将数据从一种形式传递到另一种形式

jQuery将选定的值从一种形式应用到另一种形式

如何从第一种形式的选定行中过滤另一种形式的表格?

如何将未包含在 datagridiew 中的其他数据库值显示为 C# 中的另一种形式?

如何在c#中将数据从一种形式传输到另一种形式?

另一种形式的调用方法/刷新数据

在 vb.net 中将数据从一种形式传递到另一种形式

我可以用另一种语言连接到数据库吗