ASP database connection code

Source: Internet
Author: User
Tags dbase mssql server

ASP database connection code

----- Access

Dim dB, Conn, connstr
DB ="Data/bluered. MDB"
Set conn = server. Createobject ("ADODB. Connection")
Connstr ="DBQ ="+ Server. mappath ("DB") +"; Defaultdir =; driver = {Microsoft Access Driver (*. mdb )};"
'Connstr ="Provider = Microsoft. Jet. oledb.4.0; Data Source ="& Server. mappath (db)
'Connstr ="Driver = {Microsoft Access Driver (*. mdb)}; DBQ ="& Server. mappath (db)
'Connstr ="DSN = bluered;"
Conn. Open connstr

----- Sqlserver

Set conn = server. Createobject ("ADODB. Connection")
On Error resume next
Connstr ="Provider = sqloledb; Password = ***; persist Security info = true; user id = sa; initial catalog = MSDB; Data Source = localhost; Connect timeout = 15" 
Conn. Open connstr

I. asp Object Access Database Method
In ASP, the objects used to access the database are collectively called ADO (Active Data Objects), which mainly contain three types of objects: connection, recordset, and command.
Connection: opens or connects data.
Recordset: used to access data tables.
Command: executes the action query command on the database.

2. Drivers connecting to various databases
You can use drivers or data sources to connect to different databases. However, we recommend that you use drivers because they are very convenient and simple, and it is troublesome to use data sources.

ODBC Link

Suitable for database type connection
Access"Driver = {Microsoft Access Driver (*. mdb)}; DBQ = *. mdb; uid = admin; Pwd = pass;" 
DBASE"Driver = {Microsoft DBASE Driver (*. DBF)}; driverid = 277; DBQ = ------------;" 
Oracle"Driver = {Microsoft ODBC for Oracle}; server = oraclesever. World; uid = admin; Pwd = pass;" 
MSSQL Server"Driver = {SQL Server}; server = servername; database = dbname; uid = sa; Pwd = pass;" 
Text MS"Driver = {Microsoft text Driver (*. txt; *. CSV)}; DBQ = -----; extensions = ASC, CSV, tab, txt; persist securityinfo = false;" 
Visual FoxPro"Driver = {Microsoft Visual FoxPro driver}; sourcetype = dBc; sourcedb = *. dBc; exclusive = no;" 
MySQL"Driver = {MySQL}; database = yourdatabase; uid = username; Pwd = yourpassword; option = 16386;" 

Oledb Link

Suitable Connection Methods for database types
Access"Provider = Microsoft. Jet. oledb.4.0; Data Source = your_database_path; user id = admin; Password = pass;" 
Oracle"Provider = oraoledb. Oracle; Data Source = dbname; user id = admin; Password = pass;" 
Ms SQL Server"Provider = sqloledb; Data Source = machinename; initial catalog = dbname; userid = sa; Password = pass;" 
Text MS"Provider = microsof. Jet. oledb.4.0; Data Source = your_path; extended Properties'Text; FMt = delimited'" 

In general, there are many databases that use access. Here I suggest you use the following method to connect to the ACCESS database:

Dim Conn
Set conn = server. Createobject ("ADODB. Connection")
Conn. Open ="Provider = Microsoft. Jet. oledb.4.0;"&"Data Source ="& Server. mappath ("../DB/BBS. MDB")

Here, ../DB/BBS. mdb is the relative path of your database! If your database and ASP files are in the same directory, you only need to write them like this:

Dim Conn
Set conn = server. Createobject ("ADODB. Connection")
Conn. Open ="Provider = Microsoft. Jet. oledb.4.0;"&"Data Source ="& Server. mappath ("BBS. MDB")

Many beginners always encounter problems when encountering database connections. However, using the above driver will not cause problems if your database path is correct.

Solved the ACCESS database path problem!
'Use dbtype = 0 or dbtype = 1 to distinguish between the AC database and the MSSQL database.
'The precautions for using the AC library are described in the program.
Dim dbtype, Conn, strconn
Dbtype = 0' 0 indicates the ACCESS database, and 1 indicates the MSSQL database.

 

 

If (dbtype = 0) then
***** ********************************
Dim dbfoldername, dbfolder_path, sitefolder
Dbfoldername = "articledata" 'name of the folder where the database is located
Dbfolder_path = server. mappath (dbfoldername) 'database path
Sitefolder = "article" 'name of the root directory of the system

If SESSION ("root") = "" then
Session ("root") = mid (dbfolder_path, 1, instr (1, dbfolder_path, sitefolder, 1)-1) & sitefolder
End if
Set conn = server. Createobject ("ADODB. Connection ")
Strconn = "driver = {Microsoft Access Driver (*. MDB)}; DBQ = "& SESSION (" root ") &"/"& dbfoldername &"/data. mdb "'connect to the database
Conn. Open strconn
'*************************************** **************************************** ***
Elseif (dbtype = 1) then
******* ******************************
Dim dbuserid, dbpassword, dbname, dbip
'Modify the following information to suit your website
Dbuserid = "sa" 'database Login Name
Dbpassword = "" 'Database Password
Dbname = "dbname" 'Database Name
Dbip = "local" 'address of the database. If it is a local database, it is: (local)

Set conn = server. Createobject ("ADODB. Connection ")
Strconn = "provider = sqloledb.1; Data Source =" & dbip & "; initial catalog =" & dbname & "; persist Security info = true; user id =" & dbuserid &"; password = "& dbpassword &"; Connect timeout = 30"
Conn. Open strconn
'*************************************** **************************************** ***
Else
************* ************************************
Response. Write "database setup error. Please contact the administrator! "
Response. End
End if
'Response. Write strconn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.