我正在用文本框中的新數據更新數據集行,然後嘗試將其更新到我的數據庫。我不斷收到此錯誤:更新命令和C#
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
我該如何解決這個錯誤?
這裏是我的代碼:
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 doens't不生成SQL命令來操縱您的數據庫。您必須使用CommandBuilder來生成這些命令:http://msdn.microsoft.com/en-us/library/tf579hcz(v=vs.110).aspx – 2014-09-19 15:51:57
更好的教程:http://csharp.net-informations .com/dataadapter/commandbuilder-sqlserver.htm – 2014-09-19 15:54:22