0

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

这是没有正确调用 da.update() 或在“查询”按钮中错误地引用某些内容的功能吗?

这是来自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);
        }
    }
}
4

4 回答 4

0

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

"SELECT books.*, trim(author.URL) as WebSite FROM books left join author on books.AuthorID = author.RecordID group by books.name;

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

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

于 2013-08-15T21:50:15.457 回答
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();
    }  
}
于 2012-10-05T02:19:33.440 回答
0

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

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

        da.Update(ds, sTable);

        dataGridView1.DataSource = ds;      

    }
于 2016-08-06T18:11:22.210 回答
0

不是最干净的,但这是一个工作示例:

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

namespace TryMariaDB_DataGridView
{
    public partial class Form1 : Form
    {
        private MySqlDataAdapter MyDA = new MySqlDataAdapter();
        private BindingSource bSource = new BindingSource();
        private DataSet dataSet = new DataSet();
        private DataTable table = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }

        private void MySQL_ToDatagridview()
        {
            string connectionString = "server = localhost; userid = root; database = BirdWatching; port = 3306; password = xxxxxx";
            MySqlConnection mysqlCon = new MySqlConnection(connectionString);
            mysqlCon.Open();

            //select command has to include the primary key column, otherwise update command will fail
            //as it does not know exactly what row is updated.
            MyDA.SelectCommand = new MySqlCommand("SELECT * from bird", mysqlCon);
            MyDA.Fill(table);   
            bSource.DataSource = table;
            dataGridView1.DataSource = bSource;
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            //calling the command builder will generate an update command for you
            MySqlCommandBuilder cb = new MySqlCommandBuilder(MyDA);
            MyDA.Update(table);
        }

        private void buttonReload_Click(object sender, EventArgs e)
        {
            table.Clear();
            MySQL_ToDatagridview();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MySQL_ToDatagridview();
        }
    }
}
于 2020-10-02T14:28:58.247 回答