简体   繁体   English

如何使用MYSQL在C#中的datagridview中插入,删除,选择,更新值

[英]How to insert,delete,select,update values in datagridview in C# using MYSQL

Below is the code to insert value into mysql database, using datagridview. 下面是使用datagridview将值插入mysql数据库的代码。 But the selectcommand is working.It is not happening since i get error stating "Column 'username' cannot be null ". 但是selectcommand是有效的。它不会发生,因为我收到错误消息,指出“列'username'不能为null”。

This error does not pop up if i use ms access database. 如果我使用ms Access数据库,则不会弹出此错误。 Can anyone help me on this. 谁可以帮我这个事。 is there any other method to do so. 还有其他方法可以这样做。


private void button1_Click(object sender, EventArgs e)
    {    //Even using functions we can easily update the datagridview
        //Select_function();

        try
        {   //The container which displays the details.
            dataGridView1.Visible = true;

            //The binding object which binds the datagridview with backend.
            BindingSource bs = new BindingSource();

            //The datatable through which data is exported to datagridview
            table = new DataTable();
            bs.DataSource = table;
            this.dataGridView1.DataSource = bs;

            MySqlConnection conn = new MySqlConnection(db);
            conn.Open();

            string s = "select *from user";
            cmd = new MySqlCommand(s, conn);

            da = new MySqlDataAdapter();
            da.SelectCommand = new MySqlCommand(s, conn);

            //There is issue in below sytax of insert command.
            MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(@username ,@password)", conn);
            insertcommand.Parameters.Add("username",MySqlDbType.VarChar,50,"username");
            insertcommand.Parameters.Add("password", MySqlDbType.VarChar, 50, "password");
            da.InsertCommand = insertcommand;


            //Demonstrates update command
            MySqlCommand updatecommand = new MySqlCommand("update user set username=@username,password=@password where (username=@username)", conn);
            updatecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
            updatecommand.Parameters.Add("@password", MySqlDbType.VarChar, 50, "password");

             da.UpdateCommand = updatecommand;



            //Demonstration of delete Command
            MySqlCommand deletecommand = new MySqlCommand("delete from user where username=@username", conn);
            deletecommand.Parameters.Add("@username", MySqlDbType.VarChar, 50, "username");
            da.DeleteCommand = deletecommand;

            da.Fill(table);
            conn.Close();

        }
        catch (Exception err) { MessageBox.Show(err.Message); } 

    }
    private void button2_Click(object sender, EventArgs e)
    {  da.Update(table);
    }

替代文字

I am rather unsure, but doesn't mysql use numbered parameters with a "?"-synatx or named parameters with a ":"-syntax instead of (mssql) at-syntax ? 我不太确定,但是mysql是否不使用带有“?”-synatx的编号参数或带有“:”-syntax而不是(mssql)at-syntax的命名参数?

Something like : 就像是 :

MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(?, ?)", conn);
insertcommand.Parameters.Add(1, MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add(2, MySqlDbType.VarChar, 50, "password");

or 要么

MySqlCommand insertcommand = new MySqlCommand("insert into user(username,password) values(:username, :pass)", conn);
insertcommand.Parameters.Add(":username", MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add(":pass", MySqlDbType.VarChar, 50, "password");

I think you need to change the names of the parameters added to the insert command to match the names specified in the INSERT SQL statement. 我认为您需要更改添加到insert命令的参数的名称,以匹配INSERT SQL语句中指定的名称。

Try: 尝试:

insertcommand.Parameters.Add("@username",MySqlDbType.VarChar,50,"username");
insertcommand.Parameters.Add("@password", MySqlDbType.VarChar, 50, "password");

Had a similar problem updating in mysql with datagrid (vb.net). 在使用datagrid(vb.net)在mysql中更新时遇到了类似的问题。 Solved it with: 解决了:

Dim cmb As New MySqlCommandBuilder(datGrid)
Me.datGrid.Update(datSet, "mysqlTableToUpdate")

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

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