1

我在这里做错了什么?我正在尝试显示来自远程数据库的数据并插入、编辑和删除数据。我要连接的数据库是一个遥控器,我成功地连接并查看了信息,但是当我插入语法来编辑数据时,我得到一个错误(见帖子底部)。我正在使用 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# 语法并不“流利”。

4

1 回答 1

1

您已经在方法中定义了适配器变量intSchedular。但是这个变量是该方法的本地变量,不能在它之外使用(退出时它将被销毁intSchedular

为了能够使用 in 中的变量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;
    }
}
于 2013-09-29T17:53:06.777 回答