使用Devexpress编辑远程MS Access数据库

时间:2013-09-29 17:24:07

标签: c# database devexpress

我在这里做错了什么?我正在尝试显示远程数据库中的数据以及插入,编辑和删除数据。我连接的数据库是一个远程,我成功地连接和查看信息但是当我插入语法来编辑数据时,我得到一个错误(见帖子的底部)。 我正在使用Devexpress Scheduler Controller查看约会以及编辑它们 这是整个代码。

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#语法并不“流利”。

1 个答案:

答案 0 :(得分:1)

您已在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;
    }
}