SQL

By HardPlant on Wednesday, 30th July 2003. More information. Comments.

SQL The Language of Databases. Today I'll teach you how to read from a database using SQL.To read from a database using sql you need to first open a connection to a database. a connection string looks something like thisSet Connection = Server.CreateObject(""ADODB.Connection"")

SQL The Language of Databases. Today I'll teach you how to read from a database using SQL.

To read from a database using sql you need to first open a connection to a database. a connection string looks something like this

Set Connection = Server.CreateObject(""ADODB.Connection"")
Connection.Open = ""DRIVER={Microsoft Access Driver (*.mdb)}; DBQ="" & Server.MapPath(""db/mydb.mdb"")

To get info from a database you have to open a record set here are a few ways to do it

SQL = ""SELECT * FROM table""
Set RS = Connection.Execute(SQL)

and another way

SQL = ""SELECT * FROM table""
Set RS = Sever.CreateObject(""ADODB.Recordset"")
RS.Open SQL, Connection

Okay now that you know how to open stuff with SQL lets talk more about SQL. The basic sql string starts with SELECT then a * or the specfic fields in the db you want to open

SQL = ""SELECT field1, field2, field3 FROM table""
or
SQL = ""SELECT * FROM table""

don't forget the order of the sql statment first you must put a select then a * or the fields you wnat then a from and thent he table name

but wait! there is more you can do with a sql select statment here is some more stuff you can do

SQL = ""SELECT TOP 5 * FROM table""

this will open the top 5 rows from a database

SQL = ""SELECT * FROM table WHERE ID=1""

this will open only the rows where the field ID = 1 you can use text with this too but don't forget to surround text in ' like this

SQL = ""SELECT * FROM table WHERE field='something'""

SQL = ""SELECT * FROM table ORDER BY date DESC""
this will order the records found by the date field

DESC = Descending order
ASC = Ascending order

There is more you can do with sql but those things will have to wait for further parts to this tutorial at the end we will build a guest book using sql asp and a access database

Grav

Written by HardPlant on Wednesday, 30 July 2003. Tagged with sql, programming, webdev. Read 2264 times. If you liked it, please give it a digg.

Don't just sit there like a lemon! Reply!

Got something to say? Now's the time to share it with the author and everybody else that reads this posting! Lemons need not apply.

edtBOX - xHTML: yes - bbcode:no
Home | Advertise | About | Contact | Legal © Oli Warner 2001—2007 Proud 9rules member