这个感觉很简单,但是要借助datagridview数据组件,用CommandBuilder对象进行更新,可以实现删除、更新、或者添加操作。比如,以sqlite数据库(与sqlserver数据库类似,体量小一些)为例:
static SQLiteConnection conn = new SQLiteConnection("Data Source=F:\\Project\\Data\\test;Version=3;");
SQLiteCommand cmd = new SQLiteCommand("select * from usertab", conn);
SQLiteDataAdapter sda = new SQLiteDataAdapter();
DataSet ds=new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
conn.Open();
sda.SelectCommand = cmd;
sda.Fill(ds,"usertab");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "usertab";
conn.Close();
}
private void button1_Click(object sender, System.EventArgs e)
{
SQLiteCommandBuilder scb = new SQLiteCommandBuilder(sda);
scb.GetUpdateCommand();
sda.Update(ds,"usertab");
}