Wednesday 4 July 2012

ADO.NET

ADO.Net is ActiveX Data Objects using .Net Technology. It is a data provider model
which is used to connect different data source such as Sql Server, MySql Database, Oracle,
Oledb, ODBC data sources. But here we use Sql Server database at backend for storing records
to the database.
Use the following namespace in C#.net program for accessing the Sql Server classes.
using System.Data;
using System.Data.SqlClient;
If you are not using these namespaces in your program. Then you cannot use the Sql Server database classes and methods for establishing connection and cannot interact with Database.
After that make a connection.There are some connection string parameters that are used for
 making connection and transection between front-end (Windows Form) to back-end
(Sql Server Database) of application.These are listed below:
    Parameters
Description

Initial Catalog : Database Name.
Data Source : Identifies the server, Could be local machine, Machine domain name.
Integrated  security    : Set the SSPI Connection with User's Windows Login.
 Intergrated Security is secure when you are on single machine doing
 development.
User ID : Name of the user configured in SQL Server.
Password : Password matching the SQL Server User ID.

Here is the Sql Command used for establishing connection.
SqlConnection con = new SqlConnection("Data source = local; Initial Catalog = Northwind;
User Id = root; Password = Password");
The purpose of creating a connection object is so you can enable other ADO.NET code to work
with a database.
The Sequece of operation occruing in whole sqlconnection life time are as follows:
1. Instantiate the Sqlconnection.
2. Open the Connection.
3. Pass the connection to other ADO.NET objects.
4. Perform the actions (database operations such as insert, select to delete to the database)
     with the other ADO.NET objects.
5. Close the connection.
Here is the simple program for inserting data to the sql database table :
using System;
using System.Data;
using System.Data.SqlClient; // here is the namespace for the ms sql server database.
class Sqlclass
  {
        static void Main()
        { // Instantiate the Connection
           SqlConnection con = new SqlConnection("Data Source = local;
           Initial Catalog = Northwind; User Id = root");
           SqlDataReader rdr = null;
            try
                {           // Open the connection
                  con.open();
                          // Pass the connection to command object
                 SqlCommand cmd = new SqlCommand("Select * from table1", con);
                         //Use the connection
                        // get query results
                  rdr = cmd.ExecuteReader();
                                 //print the each record of the table
                          while(rdr.Read())
                             {
                               Console.WriteLine(rdr[0]);
                               }
                       }
                       finally
                       {
                           // close the reader
                        if(rdr!=null)
                       {
                          rdr.Close();
                         }
                        if(con!=null )
                           {
                                 // close the connection
                             con.Close();
                             }
                        }
                      }
                 }