我正在尝试从 datagridview 更新 MS Access 数据库。
单击按钮即可填充 datagridview,并在修改任何单元格时更新数据库。
我一直在使用的代码示例在表单加载时填充并使用 cellendit 事件。
private OleDbConnection connection = null;
private OleDbDataAdapter dataadapter = null;
private DataSet ds = null;
private void Form2_Load(object sender, EventArgs e)
{
string connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\\Users\\Peter\\Documents\\Visual Studio 2010\\Projects\\StockIT\\StockIT\\bin\\Debug\\StockManagement.accdb';Persist Security Info=True;Jet OLEDB:Database Password=";
string sql = "SELECT * FROM StockCount";
connection = new OleDbConnection(connetionString);
dataadapter = new OleDbDataAdapter(sql, connection);
ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "Stock");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Stock";
}
private void addUpadateButton_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
dataadapter.Update(ds,"Stock");
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
我收到的错误是
传递 DataRow 集合时更新需要有效的 UpdateCommand
与修改的行。
我不确定该命令需要去哪里以及如何引用单元格来更新数据库中的值。
dbDataAdapterClass(OleDbDataAdapter 继承自的类)具有 SelectCommand、UpdateCommand 和 InsertCommand。当您显式调用任何方法(例如 update ;) )时,这是负责选择、更新和插入的方法。
由于在您的代码中,您从未提供解释如何进行更新的命令,因此数据适配器不知道如何执行此操作。
因此,满足要求,向适配器添加更新命令。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)