Database Connectivity and Data Access
Posted: 2002-06-01
By: ArchiveBot
Viewed: 65
Filed Under:
No attachments for this post
This article shows various connection strings, used to connect to various databases in Windows, as well as methods to access and modify data. Some connection strings may require client software to be installed, but most work with Windows 2000.
Original Author: Daniel M. Hendricks
Code
This reference will show you how to connect to a variety of Before you can access your database, you need to connect to it Microsoft Access 2000 Database (OLE-DB): Set db = Microsoft Access databases are quick, easy, and portable. Microsoft Access 2000 Database: Set db = For a description of the difference between OLE-DB and Connecting to a database using a DSN: Set db = Before you can use this method, you must create a DSN Connect to a SQL Server database with OLE DB: Set db = An OLE DB connection can provide faster performance than a Connect to a MySQL Database Under Linux/Chili!Soft ASP: Set db = This code has only been tested on a Cobalt RAQ with Connect to Oracle 8 (OLE-DB): Set db = This code has only been confirmed to work with Oracle 8i Connect to Oracle 8: Set db = This also requires the Oracle client tools be Now that you have a connection to your database, you can run Delete Records: db.execute("DELETE FROM mytable WHERE This is only used as an example. You will need to Insert Records: db.execute("INSERT INTO mytable VALUES Again, this is only used as an example. Change the List Records: set rs=db.execute("SELECT * FROM The first line is a select statement that selects records. Add, list, and delete records: This code will open the database, add the values "Dan Here is another quick and easy way to connect and list 'This code connects to the 'This code iterates through the 'This code deletes a record, and NOTE: This does not use the same connect statementsDatabase Connectivity in ASP
databases in different ways:
using one of the following methods:
Server.CreateObject("ADODB.Connection")
db.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &
Server.MapPath("database.mdb") & ";"
It works good for small, intradepartmental applications. If you plan on
having more than a few users connecting to it, however, you many wish to
consider using a database like SQL Server or Oracle instead. Here is
another way to connect to a Microsoft Access database:
Server.CreateObject("ADODB.Connection")
db.Open "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" &
Server.MapPath("database.mdb") & ";UID=;PWD="
ODBC, check out this article
at oledb.com.
Server.CreateObject("ADODB.Connection")
db.Open "DSN=mydsn;UID=username;PWD=password"
in your control panel (usually under ODBC or Data Sources). This process
varies from each version of Windows, so you're on your own. When you
create a DSN, you will be asked to give it a name. The name you enter
should replace the "mydsn" value above, along with the username and
password.
Server.CreateObject("ADODB.Connection")
db.Open "Provider=SQLOLEDB; Data Source=SERVER; Initial Catalog=database;
User ID=username; Password=password"
DSN. This method doesn't require you to set up a DSN (which makes reloading the
machine easier), which makes it easier to reload the computer and doesn't
require you to create a DSN. However, if you move your applications to
another server or if you move your database to another server, you will need to
update any hard-coded values. There are ways around this, but for
simplicity, I have provided the example above.
Server.CreateObject("ADODB.Connection")
db.Open "Driver={MySQL}; SERVER=localhost; DATABASE=database; UID=username;
PWD=password"
Chili!Soft ASP and MySQL.
Server.CreateObject("ADODB.Connection")
db.Open "Provider=OraOLEDB.Oracle;User ID=user;Password=pwd; Data Source=hoststring;"
server and Windows client. Important: Requires Oracle client connectivity
tools to be installed. Here is another way to connect to an Oracle
database:
Server.CreateObject("ADODB.Connection")
db.Open "Driver={Microsoft ODBC for Oracle};UID=user;PWD=password;CONNECTSTRING=hoststring"
installed. For a description of the difference between OLE-DB and ODBC,
check out this article at
oledb.com.
SQL statements:
FullName = 'John Doe'")
replace "mytable" with the name of the table you are trying to delete
from. Likewise, replace "FullName" with the name of the
appropriate field.
('John Doe', 22, '321 Disk Dr.', 'Hollywood, CA')
statement as needed.
mytable")
rs.MoveFirst
Do Until rs.EOF
Response.Write rs("MyField") & "<br>"
Loop
The following lines iterate through each line, displays the current value of the
"MyField" field, and adds a line-feed. You will want to change
the "mytable" and "MyField" values appropriately.
Set db = Server.CreateObject("ADODB.Connection")
db.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" &
Server.MapPath("database.mdb") & ";"
db.execute("INSERT INTO MyTable VALUES ('Dan Hendricks', 22)")
set rs=db.execute("SELECT * FROM MyTable")
rs.MoveFirst
Do Until rs.EOF
Response.Write rs("NAME") & "<br>"
rs.MoveNext
Loop
db.execute("DELETE FROM MyTable WHERE NAME = 'Dan Hendricks'")
Hendricks" and "22" into the first two field of the chosen table,
display all current records in the table, and finally delete the record that was
added.
records:
database.
set rs=Server.CreateObject("ADODB.Recordset")
db="DSN=TechSupport;UID=TechSupport;PWD=foobar"
current records.
mySQL = "SELECT * from chairs "
rs.open mySQL, db, 1, 3
rs.MoveFirst
Do Until rs.EOF
Response.Write rs("MyField") &
"<br>"
rs.MoveNext
Loop
then adds a new one
rs.MoveFirst
rs.Delete
rs.AddNew
rs("Name") = 'Jane Doe'
rs.Update
rs.Close
listed above. It's just a different way to connect to a database and list,
add, or remove records.
Comments on this post
No comments have been added for this post.
You must be logged in to make a comment.