Updating with a SQL query - CSharp Database

CSharp examples for Database:SQL Command

Description

Updating with a SQL query

Demo Code

using System;//w  w w .  ja va2 s .  c o m
using System.Data;
using System.Data.OleDb;
public class Customers
{
   public static void Main()
   {
      Customers myCustomer = new Customers();
      myCustomer.Add("K", "R", DateTime.Now.Date);
      myCustomer.Add("K", "R", DateTime.Now.Date);
   }
   public void Add( string FirstName, string LastName, DateTime Joined)
   {
      int rv = 0;
      OleDbConnection myConnection = null;
      string  myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "User Id=;Password=;" + @"Data Source=C:\Videos.mdb";
      string  myAddQuery =  @"INSERT INTO [Names] " + @"(FirstName, LastName, JoinDate ) " + "VALUES ( \"" + FirstName + "\",\"" + LastName + "\",\"" + Joined + "\")";
      try
      {
         myConnection = new OleDbConnection(myConnectionString);
         OleDbCommand myCommand = new OleDbCommand(myAddQuery, myConnection);
         myConnection.Open();
         rv = myCommand.ExecuteNonQuery();
      }
      catch (OleDbException e )
      {
         Console.WriteLine("OleDb Error: {0}", e.Message );
      }
      finally
      {
         if ( myConnection != null )
            myConnection.Close();
      }
      Console.WriteLine("Record added ({0})", rv );
   }
}

Result


Related Tutorials