如题
代码如下
DataSet ds = new DataSet();
string strconn = "Provider=Microsoft.Jet.OleDb.4.0;"+"Data Source= " +filepath+dbname+ ";";
OleDbConnection connection = new OleDbConnection(strconn);
connection.Open(); try
{
string constr = " SELECT * FROM " + this.comboBox3.SelectedItem.ToString();
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = new OleDbCommand(constr, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad); ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
DataTable dt = ds.Tables[this.comboBox3.SelectedItem.ToString()];
dt.AcceptChanges();
ds.AcceptChanges();
ad.UpdateCommand = builder.GetUpdateCommand();
connection.Close();
ad.Update(ds, this.comboBox3.SelectedItem.ToString());
MessageBox.Show("Update Successfully!"); }
catch (Exception err)
{
MessageBox.Show(err.Message);
}
没有错误提示,就是数据库中数据不改变
谢谢
代码如下
DataSet ds = new DataSet();
string strconn = "Provider=Microsoft.Jet.OleDb.4.0;"+"Data Source= " +filepath+dbname+ ";";
OleDbConnection connection = new OleDbConnection(strconn);
connection.Open(); try
{
string constr = " SELECT * FROM " + this.comboBox3.SelectedItem.ToString();
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = new OleDbCommand(constr, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad); ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
DataTable dt = ds.Tables[this.comboBox3.SelectedItem.ToString()];
dt.AcceptChanges();
ds.AcceptChanges();
ad.UpdateCommand = builder.GetUpdateCommand();
connection.Close();
ad.Update(ds, this.comboBox3.SelectedItem.ToString());
MessageBox.Show("Update Successfully!"); }
catch (Exception err)
{
MessageBox.Show(err.Message);
}
没有错误提示,就是数据库中数据不改变
谢谢
ds.AcceptChanges();
这两行要放在ad.Update()之后~
adapter.SelectCommand = new OleDbCommand(queryString, connection);
OleDbCommandBuilder cb = new OleDbCommandBuilder(adapter); connection.Open(); adapter.Fill(dataSet, tableName);
*************************************************************************
//一定要加上对于dataset进行更新的操作,比如说:
//dataSet.Tables[talbeName].Rows[0]["Sno"]="1111111";
*************************************************************************
adapter.Update(dataSet, tableName); connection.Close();
如果你不对Dataset进行更新,试问又怎么对数据库进行更新呢...另外,还有一点就是你用CommandBuilder,表一定要有主键,不然用不了的...
ds.AcceptChanges();
放在ad.Update()之后可是数据库的数值还是没有改动
比如我只对datagrid中“Para" 列作改动, 数据库表中有一个自动增长的主键id
代码:
ad.UpdateCommand = new OleDbCommand("UPDATE " + this.comboBox3.SelectedItem.ToString() + " SET Para = @a WHERE id = @b", connection);
ad.UpdateCommand.Parameters.Add("@a",OleDbType.Double, 3, "Para");
OleDbParameter parameter = ad.UpdateCommand.Parameters.Add("@b", OleDbType.Integer, 3, "id");
parameter.SourceVersion = DataRowVersion.Original
是这个原因吗
adapter.Fill(dataSet, tableName);
*************************************************************************
//一定要加上对于dataset进行更新的操作,比如说:
//dataSet.Tables[talbeName].Rows[0]["Sno"]="1111111";
*************************************************************************
adapter.Update(dataSet, tableName);
dataSet.Tables[talbeName].Rows[0]["Sno"]="1111111";
只能用于特定某行的改动,
=============
DataSet ds = new DataSet();
string strconn = "Provider=Microsoft.Jet.OleDb.4.0;"+"Data Source= " +filepath+dbname+ ";";
OleDbConnection connection = new OleDbConnection(strconn);
connection.Open();try
{
string constr = " SELECT * FROM " + this.comboBox3.SelectedItem.ToString();
OleDbDataAdapter ad = new OleDbDataAdapter();
ad.SelectCommand = new OleDbCommand(constr, connection);
OleDbCommandBuilder builder = new OleDbCommandBuilder(ad);ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
ad.Fill(ds, this.comboBox3.SelectedItem.ToString());
DataTable dt = ds.Tables[this.comboBox3.SelectedItem.ToString()];ad.UpdateCommand = builder.GetUpdateCommand();
connection.Close();
ad.Update(ds, this.comboBox3.SelectedItem.ToString());
dt.AcceptChanges();
ds.AcceptChanges();
MessageBox.Show("Update Successfully!");}
catch (Exception err)
{
MessageBox.Show(err.Message);
}