【问题标题】:To Edit the DataGridview and also save it in a database table using c#使用 c# 编辑 DataGridview 并将其保存在数据库表中
【发布时间】:2016-03-14 09:37:38
【问题描述】:

我使用 MYSQL Server 作为我的项目后端。我有一个从数据库中填充数据的 DataGridView。当我在 DataGridView 单元格中进行更改并单击 saveButton 时,数据需要在 DataGridView 和数据库表中更改。

这里是我的编码:

using MySql.Data.MySqlClient;

using System;

using System.Data;

using System.IO;

using System.Windows.Forms;


namespace datagridview

{

public partial class Form1 : Form

{

DataTable datatab;

 MySqlDataAdapter mydtadp;

    MySqlCommandBuilder cmbl;

String MyConnection = "SERVER=*****;" +

            "DATABASE=****;" +

            "UID=root;" +

            "PASSWORD=pws";

public Form1()

    {

        InitializeComponent();

    }

 private void Form1_Load(object sender, EventArgs e)

    {

 MySqlConnection MyConn = new MySqlConnection(MyConnection);

        MyConn.Open();

        MySqlCommand comand = new MySqlCommand("select * from aster_scripts;", MyConn);

        datatab = new DataTable();

        mydtadp = new MySqlDataAdapter(comand);

        mydtadp.Fill(datatab);

        dataGridView1.DataSource = datatab;

        MyConn.Close();

    }

 private void BtnSave_Click(object sender, EventArgs e)

    {

        DataSet ds = new DataSet();

        cmbl = new MySqlCommandBuilder(mydtadp);

        mydtadp.Update(datatab);

        MessageBox.Show("SAVED");

}

}

}

我在 mydtadp.Update(datatab); 如何做到这一点?

【问题讨论】:

  • 不要使用 select * from aster_scripts,使用 Select Column names from aster_scripts。当您使用 * 时,它将获取模式,并且当您获取大量记录时,此查询将导致性能问题。

标签: c# mysql datagridview


【解决方案1】:

您可能需要更新您的 sql 查询:

select * from aster_scripts);

成为

select * from aster_scripts

您在查询中包含了一个右括号')'。

【讨论】:

    【解决方案2】:

    请注意模块级变量。如果需要,请参考此 MSDN 文章 Here

    架构

    create table aster_scripts
    (   id int auto_increment primary key,
        i int not null,
        sThing varchar(30) not null
    );
    
    insert aster_scripts (i,sThing) values (8,'frog'),(11,'cat');
    

    c#

    using System;
    using System.Data;
    using System.Windows.Forms;
    using MySql.Data.MySqlClient;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form2 : Form
        {
            private MySqlDataAdapter mydtadp = new MySqlDataAdapter();
            private BindingSource bindingSource1 = new BindingSource();
    
            MySqlCommandBuilder cmbl;
            String MyConnection = "SERVER=hostname;" +
                        "DATABASE=dbname;" +
                        "UID=dbuser;" +
                        "PASSWORD=fffff";
    
            public Form2()
            {
                InitializeComponent();
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                MySqlConnection MyConn = new MySqlConnection(MyConnection);
                MyConn.Open();
    
                mydtadp.SelectCommand=new MySqlCommand("select * from aster_scripts", MyConn);
                cmbl = new MySqlCommandBuilder(mydtadp);
    
                DataTable table = new DataTable();
                mydtadp.Fill(table);
    
                bindingSource1.DataSource = table;
                dataGridView1.DataSource = bindingSource1;
            }
    
            private void BtnSave_Click(object sender, EventArgs e)
            {
                mydtadp.Update((DataTable)bindingSource1.DataSource);
    
                MessageBox.Show("SAVED");
            }
        }
    }
    

    更改数据点击保存屏幕截图

    数据库条目

    mysql> select * from aster_scripts;
    
    +----+----------+--------+
    | id | i        | sThing |
    +----+----------+--------+
    |  1 |        8 | frog   |
    |  2 | 11333322 | cat    |
    +----+----------+--------+
    

    【讨论】:

    • 非常感谢。您为我的代码工作更多,但对不起先生。单击按钮时我遇到了一个问题,我得到了这样的注释(附加信息:不支持不返回任何键列信息的 SelectCommand 的 UpdateCommand 的动态 SQL 生成。)
    • show create table aster_scripts 说什么(例如,通过edit 提出问题)...不是在 cmets 中
    • 谢谢你,先生....我只是在mysql编码中犯了错误,现在我改变了谢谢你节省我的时间。
    猜你喜欢
    • 2023-03-17
    • 2015-05-25
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多