关于UPdate更新数据库的问题。还请各位热心帮忙解决,先谢了!!!!
Update方法如何使用啊,看书上说是和CommandBuilder联合起来用的?
我没用过不知如何使用啊,请各位高手帮帮忙!写的示例,在此先谢过了!
------解决方案--------------------呵呵 昨天才写了一个!给你发个简单的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 实验
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlDataAdapter da;
private DataSet ds;
private SqlCommandBuilder builder;
private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection( "server=(local); " + "integrated security=SSPI; " + "database=henry "))
{
da = new SqlDataAdapter( "select * from login ", con);
builder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "login ");
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "login ";
}
}
private void button1_Click(object sender, EventArgs e)
{
da.Update(ds, "login ");
}
}
}
------解决方案--------------------string sqlstr = "select * from tablename ";
SqlDataAdapter da = new SqlDataAdapter(sqlstr);
DataSet ds = new DataSet();
da.Fill(ds, "tablename ");
SqlCommandBuilder cb = new SqlCommandBuilder(da);
/*对DATASET数据修改等等*/
da.Update(ds);
ds.AcceptChanges();