【问题标题】:Using Datagridview to insert from one table to Another使用 Datagridview 从一个表插入到另一个表
【发布时间】:2018-04-03 07:20:51
【问题描述】:

我正在尝试将值从一个表插入到另一个表中,并且不知何故我试图从 datagridview 中插入值,对此非常新手,我似乎没有正确理解它。

我的代码如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace PeaceGate
{
    public partial class ScheduledAppointments : Form
    {
        string constring = ConfigurationManager.ConnectionStrings["myDatabaseConnection"].ConnectionString;
        MySqlDataAdapter mdap;
        DataSet ds;
        public ScheduledAppointments()
        {
            InitializeComponent();
            toolStripStatusLabel1.Text = "Connected as Gate";
        }

        private void ScheduledAppointments_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'visitor_managerDataSet1.scheduled_appointments' table. You can move, or remove it, as needed.
            this.scheduled_appointmentsTableAdapter.Fill(this.visitor_managerDataSet1.scheduled_appointments);

        }

        private void confirmAppointmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                 id.Text = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
                visit_time.Text = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
                visit_date.Text = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
                fullname.Text = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
                visitor_address.Text = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
                visitor_city.Text = dataGridView1.SelectedRows[0].Cells[5].Value + string.Empty;
                visitor_telephone.Text = dataGridView1.SelectedRows[0].Cells[6].Value + string.Empty;
                id_method.Text = dataGridView1.SelectedRows[0].Cells[7].Value + string.Empty;
                organization.Text = dataGridView1.SelectedRows[0].Cells[8].Value + string.Empty;
                visit_type.Text = dataGridView1.SelectedRows[0].Cells[9].Value + string.Empty;
                reason.Text = dataGridView1.SelectedRows[0].Cells[10].Value + string.Empty;
                person_visit.Text = dataGridView1.SelectedRows[0].Cells[11].Value + string.Empty;
                pictureBox1.Image = byteArrayToImage((byte[])dataGridView1.CurrentRow.Cells[12].Value);
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            }
        }

        private Image byteArrayToImage(byte[] byteArray)
        {
            MemoryStream ms = new MemoryStream(byteArray);
            Image img = Image.FromStream(ms);
            return img;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                MySqlConnection con = new MySqlConnection(constring);
                string sql = "select * from scheduled_appointments where id ='"+id.Text+"'";
                mdap = new MySqlDataAdapter(sql,con);
                MySqlCommandBuilder cmbl = new MySqlCommandBuilder(mdap);
                mdap.Update(ds, "final_appointments");
                MessageBox.Show("Appointment Confirmed!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}

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

【问题讨论】:

  • 你有一些问题,1)你没有从“final_appointments”中读取任何数据2)你没有添加任何数据

标签: c# mysql datagridview


【解决方案1】:

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

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

你可以用

做所有的服务器端
INSERT INTO final_appointments select * from scheduled_appointments where id = @id;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2014-07-29
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多