Connection Between ASP.Net and SQL-Server using SQLCommand

In this ASP.Net tutorials we are going to learn How Make connection between ASP.Net and SQL-Server using SQLCommand.

There are many method to make connection between ASP.Net and SQL-Server.

1.Connection using TableAdapter and StoredProcedure thought Wizards.
2. Connection using SQLCommand  and SQLDataAdapter using StoredProcedure .
3. Connection using SQLDataAdapter and SQLConnection writing query in webpage.

Here, We will learn to make connection between ASP.Net and SQL-Server using SQLDataAdapter, SQLConnection and SQLComand using SQL StoredProcedure.

STEP 1 – Create new website in ASP.Net. Write below code for Design Webpage:

<table>
<tr>
<td>
&nbsp;</td>
<td>
<asp:Button ID=”btnsave” runat=”server” Font-Bold=”True”
onclick=”btnsave_Click” Text=”GET DATA” />
</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td>
<asp:GridView ID=”GridView1″ runat=”server”>
</asp:GridView>
</td>
</tr>
</table>

– The ASP.Net Design Output is:

Connection between ASP.Net and SQL-Server using SQLCommand and StoredProcedure
Connection between ASP.Net and SQL-Server using SQLCommand and StoredProcedure

STEP 2 – Now Open the SQL-Server and Create a New DataBase.

sql server
sql server

– Now Open the SQL-Server and Create New Database like below:

sql server
sql server

– Now, After Creating a New Database create a New Table like Below:

Create a New Table in SQL-Server
Create a New Table in SQL-Server

 

Create a New Table in SQL-Server
Create a New Table in SQL-Server

– Now create a New StoredProcedure in SQL-SERVER like below:

Create a New StoredProcedure in SQL-Server
Create a New StoredProcedure in SQL-Server

– Create a Stored Procedure for SELECT:

Create a New StoredProcedure in SQL-Server
Create a New StoredProcedure in SQL-Server

STEP 3 -After doing this things write below code for connectivity.

Import below namespace first:

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

– Write below asp.net code on button click event for connectivity:

 string strconn = “Data Source=.\\SQLEXPRESS;Initial Catalog=’BOOK’;Integrated Security=True”;

SqlConnection sqlconn = new SqlConnection(strconn);
SqlCommand sqlcmd = new SqlCommand(“AddressBook_SELECT”, sqlconn);

SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);
DataTable DT = new DataTable();
adapter.Fill(DT);

GridView1.DataSource = DT;
GridView1.DataBind();

– The ASP.Net Connection Output like:

Connection between ASP.Net and SQL-Server using SQLCommand and StoredProcedure
Connection between ASP.Net and SQL-Server using SQLCommand and StoredProcedure

– I hope you will enjoy this ASP.Net tutorial for Connectivity.

Leave a Reply

Your email address will not be published. Required fields are marked *