-3

I'm unable to write new entries to a local Access 2002-2003 (.mdb) database using C#, the problem is the syntax of my cmdText property in the OleDbCommand class.

PLEASE DO NOT mention Parametized SQL statements or SQL Injection, since this is only for personal tests

Anyway, here's the command text I'm trying to use:

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Daniel\source\repos\DatabaseInteraction2\DatabaseInteraction2\SimpleDatabase.mdb";

        OleDbCommand command;

        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {
            connection.Open();
            command = new OleDbCommand("Insert Into SimpleTable (userName,userPassword) Values " +
            "('TestUsername','TestPassword')", connection);

            try
            {
                command.ExecuteNonQuery();
            }
            catch(Exception error)
            {
                Console.WriteLine(error.Message
            }
        }

What is the correct way to perform insertions in an Access dataset? What syntax should I use? Right now, I tried it in 6 different ways, and none of them worked.

The exceptions I get are somewhat similiar to the following:

  • Unknown parameters
  • Directory missing
  • Syntax error in "Insert Into"
Stacklysm
  • 233
  • 2
  • 11
  • @mjwills If the wrong way is not preventing SQL Injections, then I don't mind that much (for now), I am in need of a simple, up-to-date example of using Microsoft's OleDb to insert data onto a dataset – Stacklysm May 17 '18 at 03:51
  • 1
    Did you try https://stackoverflow.com/questions/10941284/how-to-insert-a-record-into-a-access-table-using-oledb ? – mjwills May 17 '18 at 03:52
  • 2
    In the code you posted, why are you creating two `OleDbCommand` objects? Why declare a variable, create a new object, assign it to the variable, then completely ignore both? If you want to learn how to work with databases then do some research on working with database. If you want help with a specific issue, provide information about that specific issue. We don't care about vague approximations of error messages produced by code that we can't see. What was the actual error message generated by the code you did post and where did it occur? – jmcilhinney May 17 '18 at 03:52
  • @mjwills Plus, my complete code is in Portuguese, it would be hard for you, the most common exception is 0x80040E14 – Stacklysm May 17 '18 at 03:53
  • "I am in need of a simple, up-to-date example of using Microsoft's OleDb to insert data onto a dataset". What exactly do you not understand about the many, many examples already available on the web? – jmcilhinney May 17 '18 at 03:53
  • "the most common exception is 0x80040E14". That's not an exception. That is an error code that means nothing to us directly. The type of the exception, the error message (translated to English if required) and the point in code it occurred is the relevant information you should be providing. – jmcilhinney May 17 '18 at 03:54

1 Answers1

1

Your code is mostly fine (you're missing ); at the end of the console.writeline). However, your connection string is wrong. Change it to...

string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Daniel\source\repos\DatabaseInteraction2\DatabaseInteraction2\SimpleDatabase.mdb";

Make sure your database exists in that location. Otherwise this works just fine.

You might want to consider using Microsoft.ACE.OLEDB.12.0 instead as Microsoft.Jet.OLEDB.4.0 is an older driver.

Ciarán
  • 3,017
  • 1
  • 16
  • 20