How to insert data into a Microsoft Access Database?

你说的曾经没有我的故事 提交于 2019-12-24 05:13:12

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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();


来源:https://stackoverflow.com/questions/6353015/how-to-insert-data-into-a-microsoft-access-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!