Asp.net database connection with sql server using c#

Asp.net sql server database connection using c#:

Sql server database connection in asp.net:

In this post we describe how to create connection from sql data base in asp.net C# web page. Here we consider basically on page connection. In asp.net we have many choices to make sql connection here we use on page connectivity from data base.

You can connect your C# application to data in a SQL Server database using the .NET Framework Data Provider for SQL Server. The first step in a C# application is to create an instance of the Server object and to establish its connection to an instance of Microsoft SQL Server.

For Make sql connection  we use fallowing steps:

Step 1:
  • Open IED make a page for text the connection.
  • Go to Server Explorer and add new data base.

Asp.net database connection with sql server using c#

  • Type the server name then authorization type then database name and click ok button.

Step 2:
  • Go to connected database and right click on name.
  • Go to propriety window.

Asp.net database connection with sql server using c#


Step 3:
  • Copy the connection string from propriety window.
  • Use this connection string for make connection in asp.net page.


Asp.net database connection with sql server using c#

Code of C# page in asp.net of connection:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         string connetionString = null;
            SqlConnection cnn ;
       // past the connection string here……………………..
                     connetionString = @"Data Source=PARIJAT-PC\PARIJAT;Initial Catalog=my_testing;Integrated Security=True";
            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();
                Label1.Text = "Connection Open ! ";
                cnn.Close();
            }
            catch (Exception ex)
            {
                Label1.Text = " can not open connection ! ";
            }
    }
}

Asp.net .aspx page code for test connection:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <h1>
   
    Database connection check and login.
   
    </h1>

    <h3> click on button and check data base connect or not .</h3>
        <p>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                Text="Check conn" />
        </p>
        <p>
            <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
        </p>
    </div>
    </form>
</body>
</html>



Asp.net database connection with sql server using c#


Other Asp.net Related post :



Comments