简体   繁体   English

C#Winforms控件-DataGridView-更新数据库

[英]C# Winforms control - DataGridView - Update Database

The programs I am using are WAMP server (and its mysql feature in particular) and MS Visual Studio 2010 and I am programming in C# 我使用的程序是WAMP服务器(尤其是它的mysql功能)和MS Visual Studio 2010,我正在C#中进行编程

Basically, here is what I need and can currently do with my application. 基本上,这是我需要的,并且目前可以与我的应用程序一起使用。

I have several datagridview 's throughout the project and the first is simple, it loads all data from a specific table in the database at the push of a button. 我在整个项目中都有几个datagridview ,第一个很简单,只需按一下按钮,它就可以从数据库的特定表中加载所有数据。 I have another form which I can insert records and have somehow managed to make a delete function which asks the user for 2 fields (first name and last name) and then it places these into a query and carries out the command. 我有另一种形式,可以插入记录,并设法制作了一个删除功能,该功能要求用户输入2个字段(名字和姓氏),然后将其放入查询并执行命令。

What do I need to do? 我需要做什么?

I need to be able to implement some way for the form to update the database. 我需要能够为表单实现某种方式来更新数据库。 I have chosen to do this through a datagridview control so the user can see what they are editting whilst they edit it. 我选择通过datagridview控件执行此操作,以便用户在编辑时可以看到他们正在编辑的内容。

I have the following code which I have tried to update the database based on the data in the datagridview control. 我有以下代码,我尝试根据datagridview控件中的数据更新数据库。

string connString = "server=localhost;User Id=root;database=collegelist;";
MySqlConnection conn = new MySqlConnection(connString);
string selectSQL = "SELECT * FROM collegeemployee";
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(selectSQL, conn);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
DataTable table = new DataTable(); 
try
{
    dgView2.Rows.RemoveAt(dgView2.CurrentRow.Index);
    da.Update(table);
}
catch (Exception exceptionObj)
{
    MessageBox.Show(exceptionObj.Message.ToString());
}

the problem with this code (listed in a method obviously) is that while the grid is able to be modified, it is unable to pass the data back to the database. 这段代码(显然是在方法中列出)的问题是,虽然可以修改网格,但无法将数据传递回数据库。

Instead of updating your database with the empty table what you should do is. 而不是使用空表更新数据库,您应该做的是。 i.Get the datasource . i。获取数据源。 like ii. 像ii。 Update/synchronize the data source and data adapter Here is the code it should work, if it doesn't please comment and tell me the problem. 更新/同步数据源和数据适配器这是应该工作的代码,如果不行,请注释并告诉我问题。

string connString = "server=localhost;User Id=root;database=collegelist;";
MySqlConnection conn = new MySqlConnection(connString);
string selectSQL = "SELECT * FROM collegeemployee";
conn.Open();
MySqlDataAdapter da = new MySqlDataAdapter(selectSQL, conn);
MySqlCommandBuilder builder = new MySqlCommandBuilder(da);
    BindingSource BindingSourceToUpdate = (BindingSource)dgView2.DataSource; // because direct casting to data table was failing in VS2o1o

                 try
                {
    dgView2.Rows.RemoveAt(dgView2.CurrentRow.Index);

                    da.Update((DataTable)BindingSourceToUpdate.DataSource);
                }
                catch(exception)
                 {
                 }
conn.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM