Einfügen, Löschen, Auswählen und Aktualisieren von Werten in datagridview in C # mithilfe von MYSQL

Unten finden Sie den Code zum Einfügen von Werten in die MySQL-Datenbank mithilfe von datagridview. Aber der selectcommand arbeitet. Es geschieht nicht, da ich die Fehlermeldung erhalte, dass "Spalte 'Benutzername' nicht null sein kann".

Dieser Fehler wird nicht angezeigt, wenn ich eine MS-Access-Datenbank verwende. Kann mir jemand dabei helfen. Gibt es eine andere Methode, um dies zu tun?

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

Antworten auf die Frage(3)

Ihre Antwort auf die Frage