使用 DevExpress 编辑远程 MS Access 数据库
本文关键字:MS Access 数据库 DevExpress 编辑 使用 | 更新日期: 2023-09-27 18:33:10
我在这里做错了什么?我正在尝试显示远程数据库中的数据并插入、编辑和删除数据。我正在连接的数据库是一个远程数据库,我成功地连接并查看了信息,但是当我插入语法以编辑数据时,我收到错误(请参阅帖子底部)。我正在使用Devexpress调度程序控制器来查看约会以及编辑它们。
这是整个代码。
public partial class MainWindow : Window
{
CarsDBDataSet dataSet;
public MainWindow()
{
InitializeComponent();
intSchedular();
}
private void intSchedular()
{
schudlerControl1.Storage.AppointmentStorage.Mappings.AllDay = "AllDay";
schudlerControl1.Storage.AppointmentStorage.Mappings.Description = "Description";
schudlerControl1.Storage.AppointmentStorage.Mappings.End = "EndTime";
schudlerControl1.Storage.AppointmentStorage.Mappings.Label = "Label";
schudlerControl1.Storage.AppointmentStorage.Mappings.Start = "StartTime";
schudlerControl1.Storage.AppointmentStorage.Mappings.Location = "Location";
schudlerControl1.Storage.AppointmentStorage.Mappings.ReminderInfo = "RemindderInfo";
schudlerControl1.Storage.AppointmentStorage.Mappings.Subject = "Subject";
schudlerControl1.Storage.AppointmentStorage.Mappings.Status = "Status";
schudlerControl1.Storage.AppointmentStorage.Mappings.Type = "EventType";
schudlerControl1.Storage.AppointmentStorage.Mappings.RecurrenceInfo = "RecurrenceInfo";
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection()
{
ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data source= ''UNDERFOOT-PC'CalUnderFootDB'CarsDB.mdb"
};
con.Open();
OleDbCommand createCommand = new OleDbCommand("select * from CarScheduling", con);
createCommand.ExecuteNonQuery();
OleDbDataAdapter adapter = new OleDbDataAdapter(createCommand);
CarsDBDataSet dataSet = new CarsDBDataSet();
// Bind the scheduler storage to appointment data.
schudlerControl1.Storage.AppointmentStorage.DataSource = dataSet.CarScheduling;
// Load data into the 'CarsDBDataSet.CarScheduling' table.
adapter.Fill(dataSet.CarScheduling);
schudlerControl1.Storage.AppointmentsInserted +=
new PersistentObjectsEventHandler(Storage_AppointmentsModified);
schudlerControl1.Storage.AppointmentsChanged +=
new PersistentObjectsEventHandler(Storage_AppointmentsModified);
schudlerControl1.Storage.AppointmentsDeleted +=
new PersistentObjectsEventHandler(Storage_AppointmentsModified);
adapter.Adapter.RowUpdated +=
new System.Data.OleDb.OleDbRowUpdatedEventHandler(adapter_RowUpdated);
}
void Storage_AppointmentsModified(object sender, PersistentObjectsEventArgs e)
{
adapter.Adapter.Update(dataSet);
this.dataSet.AcceptChanges();
}
private void adapter_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e)
{
if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
{
int id = 0;
using (OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", adapter.Connection))
{
id = (int)cmd.ExecuteScalar();
}
e.Row["ID"] = id;
}
}
}
我得到的错误来自"void Storage_AppointmentsModified"中的适配器一词,说"名称'适配器'在当前上下文中不存在"。我知道我必须定义适配器,但是如何定义?我是 C# 的新手,所以我不"流利"编写 C# 语法。
您已在 intSchedular
方法中定义了适配器变量。但是这个变量是该方法的本地变量,不能在它之外使用(当你退出intSchedular
时它将被销毁)
为了能够在Storage_AppointmentsModified
中使用变量,您需要在全局类范围内定义它,就像您已经对CarsDBDataSet
所做的那样
public partial class MainWindow : Window
{
CarsDBDataSet dataSet;
OleDbDataAdapter adapter;
.....
private void intSchedular()
{
.....
adapter = new OleDbDataAdapter(createCommand);
.....
}
void Storage_AppointmentsModified(object sender, PersistentObjectsEventArgs e)
{
this.adapter.Update(dataSet);
this.dataSet.AcceptChanges();
}
代码中要修复的另一件事是连接的处理。应按照精确的模式使用连接。创建、打开、使用、关闭和销毁。您应该在 intSchedular 中使用 using 语句,以确保连接被破坏
private void intSchedular()
{
// CREATE
using(OleDbConnection con = new OleDbConnection(... con string here....))
using(OleDbCommand createCommand = new OleDbCommand("select * from CarScheduling", con))
{
// OPEN
con.Open();
// NO USING HERE BECAUSE WE WANT THE ADAPTER OUTSIDE OF THIS METHOD
adapter = new OleDbDataAdapter(createCommand);
// USE
....
adapter.Fill(dataSet.CarScheduling);
....
} // CLOSE + DISPOSE
}
同样的事情应该在adapter_RowUpdated
private void adapter_RowUpdated(object sender, System.Data.OleDb.OleDbRowUpdatedEventArgs e)
{
if (e.Status == UpdateStatus.Continue && e.StatementType == StatementType.Insert)
{
int id = 0;
using (OleDbConnection con = new OleDbConnection( .... con string here ....))
using (OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", con))
{
con.Open();
id = (int)cmd.ExecuteScalar();
}
e.Row["ID"] = id;
}
}