如何在VS 2005中的GridView控件通过数据绑定的方法给数据库增加一行数据
3个回答
展开全部
首先要保证数据库中,你要修改的那个表,没有外键关系.
利用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"]);
}
利用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"]);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询