如何在VS 2005中的GridView控件通过数据绑定的方法给数据库增加一行数据

来源:百度知道 编辑:UC知道 时间:2024/06/03 07:26:37

首先要保证数据库中,你要修改的那个表,没有外键关系.

利用SqlCommandBuilder 自动生成删改查的语句.

以下是刚写的例子:
数据库名:demo 表名:aa Sqlserver2005
button1 查询数据库 button2 更新数据库
SqlDataAdapter adapter;
SqlCommandBuilder build;
DataSet ds;
SqlConnection con;
private void button1_Click(object sender, EventArgs e)
{
con = new SqlConnection();
con.ConnectionString = @"data source = .\sqlexpress;database = demo;integrated security = true";
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from aa";
adapter = new SqlDataAdapter(cmd);
build = new SqlCommandBuilder(adapter);
ds = new DataSet();
adapter.Fill(ds,"aa");
dataGridView1.DataSource = ds.Tables["aa"];

}

private void button2_Click(object sender, EventArgs e)
{
adapter.Update(ds.Tables["aa"]);
}

没明白