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