C#MySQL更新 - Datagridview

问题描述 投票:0回答:3

C# Windows窗体中,我有两个按钮;查询和更新。在那个表格上是一个datagridview,我把我的MySQL结果。在分离中,当我单击查询时,我得到了正确的结果。当我更改datagrid中的值并单击Update时,MySQL会收到这些更新。但是,当我返回单击Query以从MySQL表中获取最新更改时,datagridview为空。我必须关闭表单并重新单击Query才能最终显示。

这是不正确调用da.update()或在Query按钮中错误地引用某些内容的功能吗?

以下是winform的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Data.Odbc;
using System.Data.SqlClient;

namespace GridAdjustments
{
    public partial class Form3 : Form
    {
        private MySqlDataAdapter da;        // Data Adapter
        private DataSet ds;                 // Dataset
        private string sTable = "Portfolio";  // Table Name

        public Form3()
        {
            InitializeComponent();
        }


        private void Query_Click(object sender, EventArgs e)
        {
            string connectionString = "SERVER=localhost;" +
                                      "UID=xxxxxx;" +
                                      "PASSWORD=xxxxx;" +
                                      "DATABASE=test";

            MySqlConnection conn = null;

            try
            {    
                conn = new MySqlConnection(connectionString);

                conn.Open();
                da = new MySqlDataAdapter("SELECT * FROM books;", conn);
                ds = new DataSet();
                da.Fill(ds, sTable);
                conn.Close(); 
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                conn.Close();
            }
            finally
            {
                dataGridView1.Refresh();

                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = sTable;

            }  
        }


        private void Update_Click(object sender, EventArgs e)
        {
            MySqlCommandBuilder cmb = new MySqlCommandBuilder(da);

            da.Update(ds, sTable);
        }
    }
}
c# mysql datagridview insert-update
3个回答
0
投票

尝试更改代码,并检查它是否适合您。

private void Query_Click(object sender, EventArgs e)
{
    try
    {    
        MySqlConnection conn = new MySqlConnection(connectionString);
        conn.Open();
        da = new MySqlDataAdapter("SELECT * FROM books;", conn);
        ds = new DataSet();
        da.Fill(ds, sTable);
        conn.Close(); 
    }
    catch (MySql.Data.MySqlClient.MySqlException ex)
    {
        MessageBox.Show(ex.Message);            
    }
    finally
    {
        dataGridView1.Refresh();

        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = sTable;

        if (conn.State == System.Data.ConnectionState.Open)
            conn.Close();
    }  
}

private void Update_Click(object sender, EventArgs e)
{
    try
    { 
        MySqlConnection conn = new MySqlConnection(connectionString);
        conn.Open();

        MySqlCommandBuilder cmb = new MySqlCommandBuilder(da);
        cmb.Connection = conn;
        da.Update(ds, sTable);
    }
    catch (MySql.Data.MySqlClient.MySqlException ex)
    {
        MessageBox.Show(ex.Message);            
    }
    finally
    {           
        if (conn.State == System.Data.ConnectionState.Open)
            conn.Close();
    }  
}

0
投票

您的方法适用于简单查询,但是您希望在表单上包含作者的URL,如下所示:

“选择书籍。*,修剪(author.URL)作为WebSite FROM书籍留在书籍上加入作者.AuthorID = author.RecordID group by books.name;

我添加了Trim()和AS,因为MySQLConnector不支持一次编辑多个表,并使其成为计算字段,直到您想要刷新它。

现在要记住,因为我没有提出解决方案,但我一直在挖掘。


0
投票

试试这个。重新绑定数据源以在更新后刷新DG。

   private void Update_Click(object sender, EventArgs e)
    {
        MySqlCommandBuilder cmb = new MySqlCommandBuilder(da);

        da.Update(ds, sTable);

        dataGridView1.DataSource = ds;      

    }
© www.soinside.com 2019 - 2024. All rights reserved.