Peter
Peter

Reputation: 21

How to insert data into a Microsoft Access Database?

I'm trying to insert data into a Microsoft Access Database.

I inserted data into the Access database, but the first and second time are the only times that show the data I inserted. When I rebuild my application, the data I inserted is gone. I don't know where they go and not show. I use C# with the .NET framework to develop. Here's the relevant part of the code:

OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["Constr"].ConnectionString);
OleDbCommand com = new OleDbCommand();
com.Connection = con;
com.CommandText = "Insert into Language(English,Type,Thai) values(@eng,@type,@thai)";
com.Parameters.AddWithValue("@eng", english);
com.Parameters.AddWithValue("@type", type);
com.Parameters.AddWithValue("@thai", thai);
con.Open();
com.ExecuteNonQuery();

I wrote that code, but I think it is strange. It doesn't show any errors or exceptions, but my data is not inserted correctly. Is this the correct way to insert data? If so, why it it not getting inserted?

Upvotes: 2

Views: 14864

Answers (4)

to StackOverflow
to StackOverflow

Reputation: 124686

When I rebuild my application, the data I inserted is gone

I suspect your database is being overwritten when the application is rebuilt.

This can happen, for example, if your application contains an MDB file that is copied to the output directory on build, and is used from the output directory.

Upvotes: 2

rubyraj
rubyraj

Reputation: 611

May be ur connection string not correct you can it by using .udl file just follow the link http://www.gotknowhow.com/articles/test-a-database-connection-string-using-notepad

You can also check the code shown below

        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\ruby\\Desktop\\screenShots\\ruby.mdb;Persist Security Info=False");
        con.Open();
        OleDbCommand cmd = new OleDbCommand("insert into raj(Name,Roll) values('XYZ',12);",con);
        cmd.ExecuteNonQuery();

Upvotes: 0

Brett Rossier
Brett Rossier

Reputation: 3472

You define parameters for the query, but I don't see anywhere those parameters are bound to actual data...

Try some simple tests that replace the variables you're passing in as parameters with actual values, so you can isolate the problem:

In other words, replace this:

com.Parameters.AddWithValue("@eng", english);
com.Parameters.AddWithValue("@type", type);
com.Parameters.AddWithValue("@thai", thai);

With something like this:

//I don't know the data types of your fields, so I'm guessing
com.Parameters.AddWithValue("@eng", "Test1");
com.Parameters.AddWithValue("@type", myEnum.Latin);
com.Parameters.AddWithValue("@thai", "Test1a");

If that works, then your problem probably lies with the english, type, and thai variables and you'll want to make sure they're getting the data you think they should be getting.

Upvotes: 0

NoviceProgrammer
NoviceProgrammer

Reputation: 3365

I think Language should be a reserve word and you should wrap it in [] brackets.

Also consider wrapping the code in using blocks, like

using (OleDbConnection con = new OleDbConnection(...))
{
    using (OleDbCommand com = new OleDbCommand(sqlString, con))
    {
        //code
    }
}

Other than this [possible issue with table name and that you are not closing your connection], I don't see anything wrong with the code.

Upvotes: 1

Related Questions