使用Datagridview从一个表插入另一个表

时间:2017-10-22 18:14:42

标签: c# mysql datagridview

我正在尝试将Value从一个表插入到另一个表中,不知怎的,我想从datagridview插入值,相当新手,我似乎无法正确使用它。

我的代码如下所示:

This is a test

我正在尝试从 scheduled_appointments 获取信息并保存到 Final_appointments ,如源代码所示,使用此事件处理程序 exitToolStripMenuItem_Click 我非常新手,请我就此提供指导。

1 个答案:

答案 0 :(得分:0)

您需要使用数据库schama和添加行构建数据表。

this.pagesContent = newA

您可以使用

执行所有服务器端
        MySqlConnection con = new MySqlConnection(constring);

        string sql = "select * from scheduled_appointments where id =@id";
        MySqlCommand command = new MySqlCommand(sql,con);
        command.Parameters.AddWithValue("@id", id.Text);
        var adapterScheduledAppointments = new MySqlDataAdapter(command);

        DataTable scheduledAppointmentsDataTable = new DataTable();
        adapterScheduledAppointments.Fill(scheduledAppointmentsDataTable);

        var scheduledAppointmentsRow = scheduledAppointmentsDataTable.Rows[0];
        // first you need to load the table to setup the data table
        // note: there "final_appointments" and not "scheduled_appointments"
        //

        sql = "select * from final_appointments limit 1'";
        command = new MySqlCommand(sql, con);

        // this is a different adapter and match with the table
        MySqlDataAdapter adapter = new MySqlDataAdapter(command);

        MySqlCommandBuilder cmbl = new MySqlCommandBuilder(adapter);
        DataTable finalAppointementsDataTable = new DataTable();
        // load data
        adapter.Fill(finalAppointementsDataTable);

        var newRow = finalAppointementsDataTable.NewRow();
        newRow["id"] = scheduledAppointmentsRow["id"];
        newRow["location"] = scheduledAppointmentsRow["location"];
        newRow["time"] = scheduledAppointmentsRow["time"];

        scheduledAppointmentsDataTable.Rows.Add(newRow);
        adapter.Update(finalAppointementsDataTable);