Sample Asp script for testing MSSQL Database Connections

You can use the below sample asp code for check the database connection.

 

<%

'declare the variables
Dim Connection
Dim ConnString
Dim Recordset
Dim SQL
Response.write ("Testing a Connection<br><br>")

'define the connection string, specify database driver
ConnString="DRIVER={SQL Server};SERVER=MSSQL_SERVER;UID=USER_NAME;" & _
"PWD=PASSWORD;DATABASE=DATABASE_NAME"

'declare the SQL statement that will query the database
SQL = "SELECT * FROM TABLE_NAME"

'create an instance of the ADO connection and recordset objects
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")

'Open the connection to the database
Connection.Open ConnString

'Open the recordset object executing the SQL statement and return records
Recordset.Open SQL,Connection

'first of all determine whether there are any records

If Recordset.EOF Then
Response.Write("No records returned.")
Else

'if there are records then loop through the fields

Do While NOT Recordset.Eof
Response.write Recordset("FIELD1")
Response.Write(" -- ")
Response.write Recordset("FIELD2")
Response.Write(" -- ")
Response.write Recordset("FIELD3")
Response.write "<br>"   
Recordset.MoveNext    
Loop
End If

'close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing

%>

 

 

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to View Database via PhpMyAdmin / ASP.Net Enterprise Manager in Odin Plesk Panel

To View Database via PhpMyAdmin / ASP.Net Enterprise Manager in Plesk, Please follow the...

Connection String for MsSQL 2008 database

Remote Database is strictly not allowed in Shared Hosting Servers. You can use the following...

Connection String for MsSQL 2012 database

Remote Database is strictly not allowed in Shared Hosting Servers. You can use the following...

Connection String for MsSQL 2014 database

Remote Database is strictly not allowed in Shared Hosting Servers. You can use the following...

Connection String for MsSQL 2016 database

Remote Database is strictly not allowed in Shared Hosting Servers. You can use the following...