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

时间:2009-10-05 09:03:03

标签: c# mysql datagridview

下面是使用datagridview将值插入mysql数据库的代码。 但是selectcommand正在工作。它没有发生,因为我收到错误声明“列'用户名'不能为空”。

如果我使用ms访问数据库,则不会弹出此错误。 谁可以帮我这个事。有没有其他方法可以这样做。


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

alt text

3 个答案:

答案 0 :(得分:3)

我不太确定,但是mysql不使用编号参数和“?” - synatx或带有“:”的命名参数 - 语法而不是(mssql)at-syntax?

类似的东西:

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

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

答案 1 :(得分:1)

我认为您需要更改添加到insert命令的参数名称,以匹配INSERT SQL语句中指定的名称。

尝试:

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

答案 2 :(得分:0)

使用datagrid(vb.net)在mysql中更新类似的问题。解决了它:

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