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 ASP.NET

Written By: Frostedblue On: 13/08/2009Time(16:45:18)

 

 

 

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]

  • DatabaseRead("YourColumnNameInTable")


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



    [Code]


    <%@ Import Namespace="System.Data.OleDb" %>

    <Script Runat="server">

    ' ----------

    ' -----

    ' Make Database Connection

    ' -----

    ' ----------

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

    Dim DatabaseConnection = New OleDbConnection(DatabaseSQLConnection)

    DatabaseConnection.Open()

    ' ----------

    ' -----

    ' Now Connect To Your Table By Running A SQL Command

    ' -----

    ' ----------

    Dim DatabaseSQL As String = "SELECT * FROM [YourTable]"

    Dim DatabaseCommand = New OleDbCommand(DatabaseSQL, DatabaseConnection)

    Dim DatabaseRead As OleDbDataReader = DatabaseCommand.ExecuteReader()

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Now Generate The Results From Your Table

    ' -----

    ' ----------

    While DatabaseRead.Read()

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

    End While

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Now Terminate Your Table Connection

    ' -----

    ' ----------

    DatabaseRead.Close()

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    ' ----------

    ' -----

    ' Finally Terminate Your Database Connection

    ' -----

    ' ----------

    DatabaseConnection.Close()

    ' ----------

    ' -----

    ' Done

    ' -----

    ' ----------

    </Script>




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

     

     

     

    Bookmark and Share

    197 

    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