using sql command with Parameters techniques insert data into database table — ADO.Net , C Sharp — ASP.Net Example
ASP.Net Example-using sql command with Parameters techniques insert data into database table

Thursday, May 19, 2011

using sql command with Parameters techniques insert data into database table

 
 
 
using System;
using System.Data;
using System.Data.SqlClient;

class UsingParameterAndCommand
 {
    public static void Main() 
{
string cn="server=localhost;database=DBTEST;uid=sa;pwd=sa";
 SqlConnection Connection = new SqlConnection(cn);
        
        SqlCommand objCmd = Connection.CreateCommand();
        objCmd.CommandText =
          "INSERT INTO ASPTable (" +
          "  CustomerID, CompanyName, ContactName" +
          ") VALUES (" +
          "  @CustomerID, @CompanyName, @ContactName" +
          ")";
        objCmd.Parameters.Add("@CustomerID", "AB001");
        objCmd.Parameters.Add("@CompanyName", "MicroSoft");
        objCmd.Parameters.Add("@ContactName", "Bill");
        Connection.Open();
        objCmd.ExecuteNonQuery();
        Console.WriteLine("Successfully added row to ASPTable ");

        Connection.Close();
    }
}

No comments:

Post a Comment

ASPdotNET-Example