在c#应用程序中使用OleDB或ODBC时出错
本文关键字:ODBC 出错 OleDB 应用程序 | 更新日期: 2023-09-27 18:09:40
我有一个windows应用程序,我有这样的代码:
private void saveToDatabase_Click(object sender, EventArgs e)
{
// Save the DataSet Appointments table to the database.
KaznetiTableAdapter ta = new KaznetiTableAdapter();
ta.Adapter.RowUpdated += new OleDbRowUpdatedEventHandler(Adapter_RowUpdated);
ta.Update(kbDataSet.Kazneti);
}
void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)
{
if (e.RecordsAffected == 0)
{
MessageBox.Show(
e.Row["Adresa"].ToString()
"Optimistic Concurrency Error - Notes Not Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Warning
);
e.Status = UpdateStatus.SkipCurrentRow;
}
}
我得到了一个错误信息:
错误1没有重载'Adapter_RowUpdated'匹配委托'System.Data.OleDb. 'OleDbRowUpdatedEventHandler '
如果我在Odbc中更改持有代码中的OleDb,我又得到了一个错误:
错误1无法隐式转换类型"System.Data.Odbc"。OdbcRowUpdatedEventHandler'到'System.Data.OleDb.OleDbRowUpdatedEventHandler'
我猜你得到的错误信息是很明显的:
不能隐式转换类型"System.Data.Odbc"。OdbcRowUpdatedEventHandler'到'System.Data.OleDb.OleDbRowUpdatedEventHandler'
那么,改变
一行void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)
void Adapter_RowUpdated(object sender,OleDbRowUpdatedEventArgs e)
编辑回复评论
那么我想你可以试试这样做:
ta.Adapter.RowUpdated += (sender, e) =>
{
if (e.RecordsAffected == 0)
{
MessageBox.Show(
e.Row["Adresa"].ToString()
"Optimistic Concurrency Error - Notes Not Saved",
MessageBoxButtons.OK,
MessageBoxIcon.Warning );
e.Status = UpdateStatus.SkipCurrentRow;
}
}
如果您使用OleDbDataAdapter
adapter.RowUpdating += new OleDbRowUpdatingEventHandler(OnRowUpdating);
创建处理器
protected static void OnRowUpdating(object sender,
OleDbRowUpdatingEventArgs args)
{
// code
}
如果您使用SqlDataAdapter
adapter.RowUpdating += new SqlRowUpdatingEventHandler( OnRowUpdating );
创建处理器
private static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs e)
{
// code
}
你可以很容易地在visual studio中生成事件通过按tab键两次当你输入+=
我认为你的委托需要是静态的:
static void Adapter_RowUpdated(object sender,OdbcRowUpdatedEventArgs e)