Five methods for connecting to the database through ASP

Source: Internet
Author: User

First-this method is most used in access
Strconn = "driver = Microsoft Access Driver (*. mdb); DBQ = "_
& Server. mappath ("aspfree. mdb ")
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open strconn

Second-this method is used in SQL Server
Strconn = "driver = {SQL Server}; description = sqldemo; server = 127.0.0.1 ;"_
& "Uid = loginid; Password =; database = database_name"
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open strconn

Third
Strconn = "driver = {Microsoft Access Driver (*. mdb )};"_
& "DBQ = F: \ Inetpub \ wwwroot \ somedir \ db1.mdb; defaultdir = "_
& "F: \ Inetpub \ wwwroot \ somedir; uid = loginid ;"_
& "Pwd = password; driverid = 25; fil = msaccess ;"
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open strconn

Fourth, using system data sources
The following uses a data source name: Example
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open "example"

Fifth, you must set the data source in ODBC on the control panel.
Set rs = server. Createobject ("ADODB. recordset ")
Rs. Open "tblname", "dsnname", 3, 3 first-this method is used in access
Strconn = "driver = Microsoft Access Driver (*. mdb); DBQ = "_
& Server. mappath ("aspfree. mdb ")
Set conn = server. Createobject ("ADODB. Connection ")
Conn. Open strconn

The following describes a database connection website, which is quite good.
Connectiong: http://tempuri.org/tempuri.html

ASP database connection code

'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 ("rootdir") = "" then
Session ("rootdir") = 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 (" rootdir ") &" \ "& 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

'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 ("rootdir") = "" then
Session ("rootdir") = 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 (" rootdir ") &" \ "& 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.