•  
  •  
  •  
  •  
  •  
  •  
  •  
1votes
 
 

How to connect Sql Database to ASP.Net page using C#?

coolcode Submitted by coolcode on Jul 15, 2010 10:14 AM, 0 Comments

The web.config file of ASP.Net web application provides a way to store the database connection string that can be accessed from any web page of the application. In the previous article we discussed about the connectionStrings element of the web.config file that enables you to declare the connection strings globally. In this sample we will discuss about the connection string required for connecting the SQL database instance of SQL Server Express Edition 2008 R2 and the C# code for establishing the connection between web page and the database.

Storing the Connection String for SQL Database

You can use the following connection string for connecting the SQL database:

<connectionStrings>
    <!-- connection string declared for connecting the web application with SQL database -->
    <!-- connection string for SQL Server Express Edition 2008 R2-->
    <add name="SqlConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=Northwind;User Id=sa;Password=;" providerName="System.Data.SqlClient" />
</connectionStrings>

The above code shows the usage of connectionStrings element and its add sub element that can be declared and initialized inside the web.config file. I have specified the Northwind database in the connection string with User Id = sa and a blank Password. You must specify the secure User Id and Password for the real time ASP.net web applications.

C# Code for Connecting the Web Page with SQL Database

// create a SqlConnection object and initialize it by passing connection string.
// ConfigurationManager class provides the ConnectionStrings collection type property
// to access the connection string stored in the web.config file.
SqlConnection myDataConnection = new SqlConnection(
    ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

// open the data connection.
myDataConnection.Open();

// display the connection's current state open/closed.
Response.Write(string.Format("<i><b>Connection state:</b> {0}</i><br />", myDataConnection.State));

// close the data connection.
myDataConnection.Close();

// again display the connection's current state open/closed.
Response.Write(string.Format("<i><b>Connection state:</b> {0}</i>", myDataConnection.State));

// dispose the connection object to release the resources.
myDataConnection.Dispose();

Namespace Required

// import the following namespaces
using System.Configuration;
using System.Data.SqlClient;

The System.Configuration namespace provides the access to the ConfiguartionManager class and the System.Data.SqlClient provides the ADO.Net Data Provider for connecting the SQL database with ASP.Net web application.

Output

It will produce the following output:

Connection state: Open
Connection state: Closed

In the next article we will learn How to connect Sql DataBase to ASP.Net page using VB.Net where we will learn about the connection string required for connecting the web application with SQL database, its storage inside the web.config file and accessing its value from the VB.Net code to access the data items.

  • Sponsored Ads

Permissions

You cannot comment to this post.
You cannot vote for this post.
You cannot vote for comments to this post.
Related Postings
published on Mon, 19 Jul 2010 05:36:50
coolcode Submitted by coolcode, 0 Comments
published on Mon, 19 Jul 2010 05:28:28
coolcode Submitted by coolcode, 1 Comments
published on Thu, 15 Jul 2010 10:22:11
coolcode Submitted by coolcode, 0 Comments
published on Thu, 15 Jul 2010 10:14:31
coolcode Submitted by coolcode, 0 Comments
published on Thu, 15 Jul 2010 10:02:11
coolcode Submitted by coolcode, 0 Comments
  • Top Members

  • simmi
  • coolcode
  • coder
  • codeblessu
  • justin
  • jessie
  • Featured Poll

Which one is the better combination to develop an ASP.Net web site?

14 vote(s)
2 vote(s)
2 vote(s)
  • Recent Members

  • Nandhana
  • madhumr
  • serdeeneil
  • vwrtyq
  • shanmuam
  • raja_raja