编辑DataGridview并使用c#将其保存在数据库表中

时间:2015-12-09 07:44:27

标签: c# mysql datagridview

我使用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");

}

}

}

我有这样的错误(附加信息:对于不返回任何键列信息的SelectCommand,不支持UpdateCommand的动态SQL生成。)行mydtadp.Update(datatab);  怎么办呢?

2 个答案:

答案 0 :(得分:2)

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

模式

public class UserController : ApiController
{
  [Route("api/user")]
  [MagicalAttributeName(Group="User")]
  IHttpActionResult GetUser() { ... }
}

public class ResumeController : ApiController
{
  [Route("api/user/resumes")]
  [MagicalAttributeName(Group="User")]
  IHttpActionResult GetResumes() { ... }
}

C#

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');

Chg数据点击保存屏幕截图

enter image description here

数据库条目

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

答案 1 :(得分:0)

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

select * from aster_scripts);

select * from aster_scripts

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