c# - OLEDB - 无法编辑 Access 数据库中的记录

标签 c# database ms-access oledb

在我的 WinForms 应用程序中,我正在连接到 Access (mdb) 数据库。我在数据库中有 2 个表。 MDB 未添加到项目中。它只是在应用程序的 exe 文件所在的文件夹中。

从应用程序中,我正在尝试添加/编辑数据库的记录。我可以连接到数据库,将记录添加到数据库,但很难编辑记录。当应用程序运行时,我发现数据库已更新为已编辑的记录,但在重新连接到数据库时,已编辑的记录不存在,即根本未编辑记录。数据库只是暂时用编辑过的记录更新,而不是永久更新。这是我的代码:-

    public static OleDbConnection SetupConnection()
    {

        mainCon = new OleDbConnection();
        mainCon.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = ServiceStn.mdb;";
        try
        {
            mainCon.Open();

            // Fill Data DS
            FillAppointmentDS();
         ........ 


    // Fill AppointmentDS with Appointment Table data
    private static void FillAppointmentDs()
    {
        string todayDt = DateTime.Now.ToString("dd/MM/yyyy");   

        aptDs = new DataSet();
        aptDa = new OleDbDataAdapter("Select * from Appointment where appointDate = #" + todayDt + "#", mainCon);
        OleDbCommandBuilder aptCmdBuilder = new OleDbCommandBuilder(aptDa);
        aptDa.Fill(aptDs, "Appointment");
        aptDa.FillSchema(aptDs, SchemaType.Source, "Appointment");

        return;
    }

    // Add new Appointment to the DB
    public static bool SaveAppointment(Models.Appointment apt)
    {
        DataRow dr = null;

        dr = aptDs.Tables[0].NewRow();

        dr["custID"] = apt.CustomerId;
        dr["appointDate"] = apt.AppointmentDate.Date;   // "17/9/2014";
        dr["appointTime"] = apt.AppointmentTime.TimeOfDay.ToString();
        dr["companyName"] = apt.AC_CompanyName;
        dr["model"] = apt.AC_Model;
        dr["problem"] = apt.AC_Problem;

        aptDs.Tables[0].Rows.Add(dr);

        int updatedRows = aptDa.Update(aptDs, "Appointment");
        Console.WriteLine("Updated Rows - " + updatedRows);
        dr = null;

        // Add Appointment to the AppointmentList
        appoints.Add(apt);

        if (updatedRows > 0)
            return true;
        else
            return false;
    }

    // Update an existing Appointment in the DB
    public static bool UpdateAppointment(Models.Appointment apt)
    {
        DataRow dr = null;

        string filter = "aptID=" + apt.AppointmentId;

        dr = aptDs.Tables[0].Select(filter)[0];

        dr["appointDate"] = apt.AppointmentDate.Date;   
        dr["appointTime"] = apt.AppointmentTime.TimeOfDay.ToString();
        dr["companyName"] = apt.AC_CompanyName;
        dr["model"] = apt.AC_Model;
        dr["problem"] = apt.AC_Problem;

     //   OleDbCommandBuilder aptCmdBuilder = new OleDbCommandBuilder(aptDa);

       // dr.AcceptChanges();
        aptDs.Tables[0].AcceptChanges();
       // dr.SetModified();
        int updatedRows = aptDa.Update(aptDs.Tables[0]); //(aptDs, "Appointment");

        //int updatedRows = aptDa.Update(aptDs, "Appointment");
        Console.WriteLine("Updated Rows - " + updatedRows);
        dr = null;
        if (updatedRows > 0)
            return true;
        else
            return false;
    }

SaveAppointment() 工作顺利,但 UpdateAppointment() 在将应用程序重新连接到数据库时不显示数据库中更新的记录。

谁能帮我知道为什么我不能将记录永久编辑到数据库中!!我的代码哪里出错了?我在代码中没有收到任何错误/异常。非常感谢任何帮助。谢谢。

最佳答案

UpdateAppointments 中的这一行

aptDs.Tables[0].AcceptChanges();

应该被删除,否则该行的状态变为 Unchanged 并且随后调用 Update 没有发现任何修改的行,因此它不能写入您对数据库的更改-

AcceptChanges 的含义总是令人困惑。

当您修改一行时,它的 RowState属性变为 Modified .
当您调用 OleDbDataAdapter.Update 方法时,它会搜索表中具有 RowState 的所有行,例如 AddedDeletedModified 并应用由 OleDbCommandBuilder 创建的适合状态的 OleDbCommand。

AcceptChanges处理这些状态,将它们改回未更改状态(并从 DataTable 容器中删除状态为已删除的行)。
这样,调用 AcceptChanges 后的更新将找不到任何符合写回数据库条件的行。

关于c# - OLEDB - 无法编辑 Access 数据库中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25946522/

相关文章:

c# - linq where call 是否减少了对我的数据库的调用(自定义构建)

php - 通过数据库缓存对于生产站点来说效率低下吗?

mysql - 在关系数据库 (MySQL) 中表示数字范围

java - 如何从 Spring JdbcTemplate 连接到受密码保护的 MS Access 数据库?

java - 无法使用 GUI Java 从 MS Access 删除数据

vba - ListView 列顺序的控制

c# - 确切地说,MemoryCache的内存限制是什么意思?

c# - 填充 ComboBox DataColumn 项和值

c# - 将数字部分从字母数字中分离出来

android - 我需要做什么才能使用 Android 连接到中央数据库?