0

I am trying to insert data into MS access table with c#. I have the following code:

OleDbConnection aConnection = new   
                        OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;
                                     Data Source=storage_db.accdb");
string sql = ("INSERT INTO Client (cname, phone, [password])  
               VALUES(@Name,@Phone,@Password)");
OleDbCommand insert = new OleDbCommand(sql, aConnection);
aConnection.Open();
insert.Parameters.Add("@Name", OleDbType.VarChar).Value = textBox1.Text;
insert.Parameters.Add("@Phone", OleDbType.VarChar).Value = textBox2.Text;
insert.Parameters.Add("@Password", OleDbType.VarChar).Value = textBox3.Text;
insert.ExecuteNonQuery();
aConnection.Close();

There is no error messages or exceptions but data doesnt appear in a table. What am I doing wrong?

Sachu
  • 7,555
  • 7
  • 55
  • 94
user3340565
  • 45
  • 1
  • 7
  • Did you debug your code? Are you sure your connection string is right? What is your query looks like after your add parameter values? Is it insert in your database manager? – Soner Gönül May 21 '15 at 11:29
  • can u print the value returned by insert.ExecuteNonQuery(); It willl show how many rows were affected.... so could be interesting information. May be you are looking to another database... – Tirma May 21 '15 at 11:31
  • Hello sorry for silence. I probably understood what is going on. Insert query actually works. Only problem is that I have added a access file as a datasource. And while adding this file I so an alert from visual studio that this file will always be copied into project's bin folder. So I inserted data checked the result without closing the project and it worked. But when I restarted the program in that new file was no new data. Do you have any ideas how to avoid this setuation? – user3340565 May 21 '15 at 12:12

0 Answers0