简体   繁体   中英

Unable to insert data in MS Access using parameterized query in c#

I have the following code which tries to store e values form 3 textboxes into a MS Access 2007 database.

string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\dxs.accdb");
string SqlString = "Insert Into tests( [Nam], [add], [phone]) Values (?,?,?)";

using (OleDbConnection conn = new OleDbConnection(ConnString))
{
   using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
   {
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.AddWithValue(@"Nam", textBox1.Text);
      cmd.Parameters.AddWithValue(@"add", textBox2.Text);
      cmd.Parameters.AddWithValue(@"phone",textBox3.Text);

      conn.Open();
      cmd.ExecuteNonQuery();
      MessageBox.Show("entered");
   }
}

But even though the code is correct after entering values nothing is being stored in table.

Shouldn't

cmd.Parameters.AddWithValue(@"Nam", textBox1.Text);

Be:

cmd.Parameters.AddWithValue("@Nam", textBox1.Text);

And so on for the other parameters?

When i had the similar problems, solution was:

If database is part of application it can be copied in a bin folder - and then application work with it. That is why you can`t find your changes in datatables with MS Access client.

Make sure your database exists in output(bin) folder where exists your exe file of project. If not then copy it there. After your have your database file at right place, You will be to see the changes.

Additionally, you also need few changes in your code, you have problem with your parameter. Change Values (?,?,?) to Values (@Nam,@add,@phone)"; and @"Nam" to "@Nam" . See the comments Correction1 and Correction2 .

Also no need to use double slash \\\\ when you are using @ at beginning of string

string ConnString=@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dxs.accdb");

string sql="Insert Into tests([Nam],[add],[phone]) Values (@Nam,@add,@phone)";
// Correction 1: Above line is changed ?,?,? to parameter names (names used by your command)

using (OleDbConnection conn = new OleDbConnection(ConnString))
{
   using (OleDbCommand cmd = new OleDbCommand(sql, conn))
   {
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.AddWithValue("@Nam", textBox1.Text);
      cmd.Parameters.AddWithValue("@add", textBox2.Text);
      cmd.Parameters.AddWithValue("@phone",textBox3.Text);
      // Correction 2: your parameter names are changed @"xyz" to "@xyz"

      conn.Open();
      cmd.ExecuteNonQuery();
      MessageBox.Show("entered");
   }
}

your insert statement should be like dis

    string SqlString = "Insert Into tests( [Nam], [add], [phone]) Values (@Nam, @add, @phone)";


   cmd.CommandType = CommandType.Text;
  cmd.Parameters.AddWithValue("@Nam", textBox1.Text);
  cmd.Parameters.AddWithValue("@add", textBox2.Text);
  cmd.Parameters.AddWithValue("@phone",textBox3.Text);

try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM