how to insert,delete update an access 2000 databSE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • preimol
    New Member
    • May 2010
    • 2

    how to insert,delete update an access 2000 databSE

    Hie people

    I have a database named person that has a table called emp and a field by the name Names.Iam using VS 2005 how do I insert,delete and update the database.

    this is my code-

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                string connetionstring = null;
                string sql = null;
                OleDbConnection cnn;
                OleDbCommand cmd;
    
                connetionstring = "Provider=Microsoft.Jet.Oledb.4.0; Data   source=person.mdb;";
                sql = "insert into emp values(' + textBox1.Text ')";
    
                cnn = new OleDbConnection(connetionstring);
                cmd = new OleDbCommand(sql);
                cmd.Connection = cnn;
    
                cnn.Open();
                try
                {
                    
                    cmd.ExecuteNonQuery();
                    
                    MessageBox.Show("record inserted");
                    
                }
                
                catch(Exception ex)
                {
                    MessageBox.Show("Can not open connection"+ex.ToString());
                }
                cnn.Close();
    
            }
    Last edited by tlhintoq; May 20 '10, 11:07 AM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Database tutorial Part 1
    Database tutorial Part 2

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Code:
      sql = "insert into emp values(' + textBox1.Text ')";
      This most certainly won't work. You are literally using the string "textBox1.Text" , not its value, in this string.

      Code:
      sql = string.Format("insert into emp values('{0}')", textBox1.Text);
      This way is better, but still not "good". Parameterized queries are the best.

      Comment

      Working...