【发布时间】:2014-09-19 15:48:49
【问题描述】:
我正在使用文本框中的新数据更新数据集行,然后尝试将其更新到我的数据库。我不断收到此错误:
当传递带有修改行的 DataRow 集合时,更新需要有效的 UpdateCommand。
我该如何解决这个错误?
这是我的代码:
protected void Save_Butt_Click( object sender, EventArgs e ) {
OleDbConnection connect = new OleDbConnection( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/PizzaOrders.mdb;Persist Security Info=True" );
//set up connection string
OleDbCommand command = new OleDbCommand("SELECT [title], [gname], [sname], [address], [suburb], [postcode], [dayphone], [email] FROM [users] WHERE ([username] = @username)", connect);
OleDbParameter param0 = new OleDbParameter("@username", OleDbType.VarChar);
param0.Value = HttpContext.Current.User.Identity.Name;
command.Parameters.Add(param0);
connect.Open();
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet dset = new DataSet();
da.Fill(dset);
dset.Tables[0].Rows[0]["title"] = Title_DDL.Text;
dset.Tables[0].Rows[0]["gname"] = Fname_txt.Text;
dset.Tables[0].Rows[0]["sname"] = LN_txt.Text;
dset.Tables[0].Rows[0]["address"] = Address_txt.Text;
dset.Tables[0].Rows[0]["suburb"] = suburb_txt.Text;
dset.Tables[0].Rows[0]["postcode"] = Postcode_txt.Text;
dset.Tables[0].Rows[0]["dayphone"] = Phone_txt.Text;
dset.Tables[0].Rows[0]["email"] = Email_txt.Text;
da.Update(dset);
}
【问题讨论】:
-
DataAdapter 不会生成 SQL 命令来操作您的数据库。您必须使用 CommandBuilder 来生成这些命令:msdn.microsoft.com/en-us/library/tf579hcz(v=vs.110).aspx