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?
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:
With something like this:
If that works, then your problem probably lies with the
english
,type
, andthai
variables and you'll want to make sure they're getting the data you think they should be getting.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.
I think Language should be a reserve word and you should wrap it in [] brackets.
Also consider wrapping the code in
using
blocks, likeOther than this [possible issue with table name and that you are not closing your connection], I don't see anything wrong with the code.
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