TECHNOLOGY BLOG

WEB DEVELOPMENT

IT PROJECTS

DESIGN PROJECTS

FORUM

CONTACT US

ABOUT US

Blog | News | Information | Articles

 

A Simple Script To Access A SQL Server Database Using Classic ASP

Written By: Frostedblue On: 11/07/2009Time(03:33:40)

 

 

 

This is a piece of code that will allow you to connect to a SQL database and loop the results into a list. This will need to be amended to suit your setup and requires SQL Server to be configured with your chosen tables.

 

Please not that this script has been written as an example, you will need to change the following to allow acces to any other SQL Server:

  • SERVERNAME\SQLSERVER

  • YOURDATABASE

  • USERNAME

  • PASSWORD

  • SELECT * FROM YourTable;

  • TableRecord("YourColumnNameInTable")


  • These can be obtained from your SQL Server Application and your setup Database.


    [Code]


    <%

    ' ----------

    ' -----

    ' Make Database Connection

    ' -----

    ' ----------

    Dim DatabasePath

    Dim DatabaseConnection

    Dim DatabaseSQLConnection

    Set DatabaseConnection = Server.CreateObject("ADODB.Connection")

    DatabaseSQLConnection = "Provider=SQLOLEDB; Data Source=SERVERNAME\SQLSERVER; Initial Catalog=YOURDATABASE; User Id=USERNAME; Password=PASSWORD;"

    DatabaseConnection.Open DatabaseSQLConnection

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Now Connect To Your Table By Running A SQL Command

    ' -----

    ' ----------

    TableSQL = "SELECT * FROM YourTable;"

    Set TableRecord = DatabaseConnection.Execute(TableSQL)

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Now Generate The Results From Your Table

    ' -----

    ' ----------

    If TableRecord.EOF Then

    Response.Write ("<p>No Records Found</p>")

    End If

    ' -----

    Do While Not TableRecord.EOF

    Response.Write ("<p>" & TableRecord("YourColumnNameInTable") & "</p>")

    TableRecord.MoveNext

    Loop

    ' ----------

    ' -----

    ' Now Terminate Your Table Connection

    ' -----

    ' ----------

    TableRecord.Close

    Set TableRecord = Nothing

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Finally Terminate Your Database Connection

    ' -----

    ' ----------

    DatabaseConnection.Close

    Set DatabaseConnection = Nothing

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    %>




    Code Written By Frostedblue For Use With IIS, ASP.NET & SQL Server

     

     

     

    Bookmark and Share

    250 

    Comments Made:

    No Comments Have Been Made, Please Leave A Comment Below.

    Reply To This Article (Registration Not Required)

    * Your Name Or Alias:

    * Your Email Address (This Is Not Published):

    * Your Comment:

    Your Website Address (Optional):

    * Authorisation Code (Please Ender This 4 Digit Code In The Box Below):

    Please Note:

     

    Your comment, if deemed unsuitable, maybe edited or deleted by our moderators ASAP. Please keep it clean and if you have a disagreement, keep it calm. Thank You.

     

    * All indicated textboxes are manditory.


    News/Blog Article Archives:

    Year: 2010

    Year: 2009



    Copyright © Frostedblue 2010, All Rights Reserved | FB:Board v1.4
    This Site Has Been Optimised For IE7+, Safari 3+ & Firefox 3+
    My Home Page     |     Log In To My Account     |     Register Now
    Anonymous User, Please Log In