Here are the steps that you need to take in order to connect and performs some operation on a database:
1)Instantiate the SqlConnection.
2)Open the connection.
3)Pass the connection to other ADO.NET objects.
4)Perform database operations with the other ADO.NET objects.
5)Close the connection.
in header file use as :
using System.Data.SqlClient;in body use as:
static void Main()
// 1. Instantiate the connection
SqlConnection connection = new SqlConnection(
"Data Source=(local);Initial Catalog=DevelopersIsland;Integrated Security=SSPI");
SqlDataReader rdr = null;
// 2. Open the connection
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from Users", connection);
// 4. Use the connection
// get query results
rdr = cmd.ExecuteReader();
// print the User ID of each record
// close the reader
if (rdr != null)
// 5. Close the connection
if (connection != null)