|
|
|
|
|
|
|
Sign In

.

ADHM


 

ADO.NET Connection -- SQL

This is sample of ADO.NET connection with SQL Database.

connectStr="server=serverAddress; uid=username; pwd=password; database=database;"

[C# - ASPX]
//For ADO.Net, Using two namespaces
using System.Data;
using System.Data.SqlClient;

//Create a new SqlConnection object with the connection string
SqlConnection sqlConn = new SqlConnection(connectStr);

//Open the connection
sqlConn.Open();

SqlCommand thisCommand = nconConnection.CreateCommand();
thisCommand.CommandText = _SqlQuery;
SqlDataReader thisReader = thisCommand.ExecuteReader();

//Retrieve Data...
while (thisReader.Read())
{
Response.Write(thisReader.GetValue(0).ToString() + "," + thisReader.GetValue(1).ToString());
}

//Close the Reader
thisReader.Close();

//Close the connection
sqlConn.Close();

[VB - ASPX]
"For ADO.Net, Import two namespaces
Imports System.Data
Imports System.Data.SqlClient

"Create a new SqlConnection object with the connection string
Dim sqlConn As New SqlConnection(connectStr)

"Open the connection
sqlConn.Open()

"Write Code to Retrieve Data...

"Close the connection
sqlConn.Close()

[VB-ASP]
[VB-ASP]

@ nvianhd 2008