简体   繁体   中英

C# Cannot save data in Access 2007

I cannot save data in access 2007. I tried the following:

  • Add a password to my DB; didn't work
  • Saved the db as a 2003 file; didn't work

Here is my code:

public bool ExecuteUDI(string query)
{
    Command = new OleDbCommand();
    Command.Connection = Connection;
    Command.CommandText = query;
    Command.CommandType = System.Data.CommandType.Text;
    try
    {
        // Open connection
        Open();

        if (Command.ExecuteNonQuery() != -1)
            return true;
        else
            return false;
    }
    catch (Exception e)
    {
        mError = "ExecuteUDI - " + e.Message;
        return false;
    }
    finally
    {
        // Always close connection
        Close();
    }
}

When I add breakpoints, I see my query looks good:

INSERT INTO DVD (Titel) VALUES ('Elegy')

I don't get any errors, but the affected rows is 0. How come? I dont understand..

Where is your mdb file located in relation to your code? I have had issues in the past that having the mdb file in the project folder will in essence create a local copy of the db in memory when the app is running, but nothing is actually written back to the mdb in the folder...

I recommend putting the mdb file outside your project's folder. That should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM