[英]C# MySQL Update - Datagridview
在C#
Windows 表格中,我有两个按钮; 查询和更新。 在该表单上是一个datagridview
,我在其中放置了 MySQL 结果。 在分离中,当我单击 Query 时,我得到了正确的结果。 当我更改datagrid
中的值并单击更新时,MySQL 会收到这些更新。 但是,当我返回单击查询以从 MySQL 表中获取最新更改时, datagridview
为空白。 我必须关闭表单并重新单击查询才能最终出现。
这是 function 没有正确调用 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);
}
}
}
尝试更改您的代码,并检查它是否适合您。
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();
}
}
您的方法适用于您在此处的简单查询 - 但假设您希望在表单上拥有作者的 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 不支持一次编辑多个表,并使其成为计算字段有效 - 直到您想要刷新它。
现在要记住这一点,因为我还没有提出解决方案,但我一直在挖掘。
试试这个。 更新后重新绑定您的数据源以刷新您的 DG。
private void Update_Click(object sender, EventArgs e)
{
MySqlCommandBuilder cmb = new MySqlCommandBuilder(da);
da.Update(ds, sTable);
dataGridView1.DataSource = ds;
}
不是最干净的,但这是一个工作示例:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.