请问我想用C# dataGridView 对数据库进行数据 update ,delete ,insert 操作,如何实现!谢谢!!

解决方案 »

  1.   

    delete
    ------------------------------------------------
    SqlConnection conn = new SqlConnection("server=WHOAMI;uid=sa;pwd=;database=tb_01");
                    conn.Open();
                        string strselect = "Select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'";
                        SqlCommand cmd = new SqlCommand(strselect, conn);
                    int intConut=Convert.ToInt32(cmd.ExecuteScalar().ToString());
                    if(intConut!=0)
                    {
                        string SqlIns = "delete from tb_01 where BookNumber='" + textBox1.Text + "'";
                        SqlCommand commad = new SqlCommand();
                        commad.CommandText = SqlIns;
                        commad.Connection = conn;
                        commad.ExecuteNonQuery();
                        MessageBox.Show("删除成功!");
                        filldataview();                }
                    else
                    {
                        MessageBox.Show("编号不正确!");
                    }
    ---------------------------------------------------------
    update
    -------------------------------------------------------
     SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = "Server=WHOAMI;uid=sa;pwd=;database=tb_01";
                    conn.Open();
                    string strselect = "select COUNT(*) from tb_01 where BookNumber='" + textBox1.Text + "'";
                    SqlCommand cmd = new SqlCommand(strselect, conn);
                    int intConut = Convert.ToInt32(cmd.ExecuteScalar().ToString());
                    if (intConut != 0)
                        {
                            string SqlIns = "update tb_01 set BookName='" + textBox2.Text + "',BookP='" + textBox3.Text + "',BookPrice='" + textBox4.Text + "' where BookNumber='" + textBox1.Text + "'";
                            SqlCommand command = new SqlCommand();
                            command.CommandText = SqlIns;
                            command.Connection = conn;
                            command.ExecuteNonQuery();
                            MessageBox.Show("修改成功!");
                            fillDataview();
                        }
                        else
                        {
                            MessageBox.Show("编号输入错误,无法修改记录!");                    }
                    }
    --------------------------------------------------------------------------
    insert
    ------------------------------------------------------------------------
     SqlConnection conn = new SqlConnection();
                        conn.ConnectionString = "Server=WHOAMI;uid=mas;pwd=mas;database=tb_01";
                        conn.Open();
                        string SqlIns = "insert into tb_01 values('')";                    SqlCommand command = new SqlCommand();
                        command.CommandText = SqlIns;
                        command.Connection = conn;
                        command.ExecuteNonQuery();
                       
                        SqlDataAdapter AdapterSelect = new SqlDataAdapter("select * from tb_01", conn);
                        DataTable dt = new DataTable();
                        AdapterSelect.Fill(dt);
                        dataGridView1.DataSource = dt.DefaultView;