更新MySql数据库 [英] Update MySql Database
问题描述
我正在使用MySql数据库;我从数据库中检索了数据,并将数据填充到了文本字段中,并对它进行了一些修改.我尝试使用data.GetChanges()命令检索对DataTable的更改;它正在检索空值!如果我要将记录位置从当前记录更改为另一条记录,它将开始生效,并且我将使用相同的命令来更改数据.我想我必须在运行GetChanges()之前执行一些命令.请帮助我解决这个问题
谢谢&问候
Thahir
代码:
Hi,
I am using MySql Database; I retrieved data from database and populated data to text field and I have made some modifications on it. And I try to retrieve changes to DataTable by using data.GetChanges() command; it is retrieving null value! If I am changing the record position from current record to another record it is getting effected and I am getting the data changes by using same command. I think I have to execute some command before running GetChanges(). Please help me on this issue
Thanks & Regards
Thahir
Code :
namespace App014
{
public partial class Form1 : Form
{
private MySqlConnection conn;
private DataTable data;
private MySqlDataAdapter da;
private MySqlCommandBuilder cb;
public Form1()
{
InitializeComponent();
ConnectDB();
}
private void ConnectDB()
{
if (conn != null)
conn.Close();
string connStr = "server=localhost;user id=root; password=xxxxx; database=eTest";
try
{
conn = new MySqlConnection(connStr);
conn.Open();
data = new DataTable();
da = new MySqlDataAdapter("SELECT code,name FROM USERMASTER", conn);
cb = new MySqlCommandBuilder(da);
da.Fill(data);
lstUser.DataSource = data;
lstUser.DisplayMember = "name";
lstUser.ValueMember = "code";
txtCode.DataBindings.Add(new System.Windows.Forms.Binding("Text", data, "code"));
txtName.DataBindings.Add(new System.Windows.Forms.Binding("Text", data, "name"));
}
catch (MySqlException ex)
{
MessageBox.Show("Error connecting to the server: " + ex.Message);
}
}
private void UpdateData()
{
DataTable changes = data.GetChanges();
if (changes != null)
{
da.Update(changes);
data.AcceptChanges();
}
else
{
MessageBox.Show("No Changes found");
}
}
private void btnSave_Click(object sender, EventArgs e)
{
UpdateData();
}
}
}
推荐答案
浏览此
使用C#和.NET连接到MySQL数据库 [ ^ ]
您会找到答案
go through this
Connecting to MySQL Database using C# and .NET[^]
you will find your answer
这篇关于更新MySql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!