ASP.Net Connect to SQL Database

by top54u.com 13 Feb, 2008
Spotlight.....

In ASP.Net 2.0 you can use SQLClient for creating SQL Database connection.
First you have to place the SQL connection string having authenticated username, password and name of the database as Initial Catalog and last important thing Data Source as IP/localhost defining the place of SQL Server Database server.

Apply the following in <configuration> section of web.config file:

 

<appSettings>
       <
add key="ConnectionString" value="Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=;"/>
<appSettings>

Import Namespaces for SQL Database Connection:

Imports System.Data
Imports System.Data.SqlClient

 

VB Code to connect to SQL Database:

 

' string variable to store the connection string
' defined in appsettings section of web.config file.
Dim connStr As String = ConfigurationManager.AppSettings("ConnectionString").ToString()

 

' Object created for SQL Connection
Dim myConnection As New SqlConnection(connStr)

 

' if condition that can be used to check the sql connection
' whether it is already open or not.
If myConnection.State = ConnectionState.Closed Then
myConnection.Open()
End If

 

' message string just to display the sql connection state.
Response.Write("ASP.Net 2.0 SQL Database Connection State: <b>" & myConnection.State & "</b>")

 

' if condition that can be used to check the sql connection
' if it is open then close it.
If myConnection.State = ConnectionState.Open Then
   myConnection.Close()
End If

 

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/28/2008 8:09:47 AM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....