Net-informations.com
SiteMap  | About    

ASP.NET Sql Server Connection

The SqlConnection Object is Handling the part of physical communication between the ASP.NET application and the SQL Server Database . An instance of the SqlConnection class in ASP.NET is supported the Data Provider for SQL Server Database.

vb.net
  Dim connectionString As String

connectionString = ConfigurationManager.ConnectionStrings("SQLDbConnection").ToString

C#

  string connectionString = ConfigurationManager.ConnectionStrings["SQLDbConnection"].ToString();

When the connection is established , SQL Commands will execute with the help of the Command Object and retrieve or manipulate the data in the database. Once the Database activities is over , Connection should be closed and release the Data Source resources .

The Close() method in SqlConnection Class is used to close the Database Connection. The Close method rolls back any pending transactions and releases the Connection from the SQL Server Database.

The following ASP.NET program connect to a database server and display the message in the Label control.

web.config

  

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
	<add name="SQLDbConnection"
		 connectionString="Server=servername; Database=pubs; User Id=username; password=password"
		 providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

You have to fill appropriate web.config database parameters

Default.aspx

  

<!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 id="Head1" runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
	</div>
	<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</form>
</body>
</html>


Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb



 ASP.NET Data Providers - Related Contents