Monday 1 April 2013

Steps to make data connectivity with Sql in asp.net


Hello Friends !! Today  I am just going to explain steps to make data connectivity  with SQL Server which all are as given  below one by one ..

Steps To Make Data Connectivity With SQL !!

There are five steps to make data connectivity which are explained as given below one by one:

  1. Create a connection object.
  2. Create Command object.
  3. Open the connection object.
  4. Execute the Sql statement in the command object.
  5. Close the connection object.


Create a Connection Object: This is the first step to make database connectivity we with the Sql Server. I just explained in briefly on my previous post.if you want to know more about Sqlconnection then click on the create connection or here.  The syntax for make connection with database as given below:

SqlConnection con = new SqlConnection ("data source=.; initial catalog=search; integrated security=true");

Create Command Object: This is the second step to make database connectivity with Sql Server. I just explain more about Sqlcommand object on my previous article if you want to learn more about Sqlcommand then click on the command object or here.The syntax for make command object with database as given below:
SqlCommand cmd = new SqlCommand ();
Execute the Sql statement in the command object: This is the third step to make database connectivity with Sql Server .Now in third step we have to pass command and connection object of the SqlConnection as given below:
SqlCommand cmd = new SqlCommand ("select *from find", connection);
Open The Connection Object: This is the fourth step to make database connectivity with Sql Server. . I just explain more about open connection on my previous article if you want to learn more about open connection then just click on the open connection or here.We just open the connection with open(); method .The method to open the connection as given below:
       connection.Open ();

Close the connection Object: This is the final step to make database connectivity with Sql Server. . I just explain more about close connection on my previous article if you want to learn more about close connection then just click on the open connection or here.We just close the connection with close(); method. The method for close the connection as given below:

       Connection.Close ();

Example to execute multiple statements: 
                  I am just taking an example of search user records on the bases of roll no which is as given below with source code and output screen

Output Screen:


Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void search_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("data source=.;initial catalog=search;integrated security=true");
            SqlCommand cmd = new SqlCommand("select *from find", con);
            con.Open();
       
                SqlDataReader dr = cmd.ExecuteReader();
             
                    dr.Read();
                    if (dr[0].ToString() == roll_no.Text)
                    {
               
                    full_name.Text = dr[1].ToString();
                    email_id.Text = dr[2].ToString();
                    user_address.Text = dr[3].ToString();
                    phone_no.Text = dr[4].ToString();
                }
                else
                {
                    MessageBox.Show("invalid id ");
                }
             con.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
     
    }
}

I hope this post will help you
Still,If you've some query regarding to this article then please post your comment.Your comment will be appropriate.

0 comments:

Post a Comment

Hey thanax alot to comment i will revert you back soon...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites