Asp connection SQL and Access data code (Random Functions in asp)

Source: Internet
Author: User
Tags mdb database

Asp connection SQL
Method 1: Copy codeThe Code is as follows: MM_conn_STRING = "Driver = {SQL Server}; server = (local); uid = sa; pwd =; database = infs ;"
Set conn = Server. Createobject ("ADODB. Connection ")
Conn. open MM_conn_STRING
Set rs = SERVER. CreateObject ("ADOBD. recordset ")
SQL = "SELECT * FROM TABLE ORDER BY ID DESC"
RS. open SQL, CONN, 3, 3

Common Function Code: Copy codeThe Code is as follows: DataServer = "jb51" 'database Server IP Address
DataUser = "jb51" 'user name for accessing the database
DataBaseName = "jb51" 'Database Name
DataBasePsw = "www.jb51.net" 'database access password
Set conn = Server. CreateObject ("ADODB. Connection ")
ConnStr = "driver = {SQL Server}; server =" & dataserver & "; UID =" & datauser & "; PWD =" & databasepsw & "; Database =" & databasename
Conn. open ConnStr
If Err Then Err. Clear: Set conn = Nothing: Response. Write "the database connection fails. Check the database parameter settings in the Conn. asp file. ": Response. End

Using ASP to connect to the ms SQL database, standard connection, commonly used is the following connection string:Copy codeThe Code is as follows: CONN. OPEN "Provider = SQLOLEDB.1 ;"&_
"Password = '" & pass_word &"';"&_
"Persist Security Info = true ;"&_
"User ID = '" & User_ID &"';"&_
"Initial Catalog = '" & db &"';"&_
"Data Source = '" & Data_Source &"';"&_
"CONNect Timeout =" & cntimeout &""

Description:
Provider = SQLOLEDB.1; database Provider. The following 1 indicates the version information. If not, the latest version is fully used.
User ID = SQL account, database account
Password = SQL account Password, database account Password
Initial Catalog = Database Name (only the name is used, and ACCESS is a data file with a specific path)
Data Source = Data Source, SQL Server name or its IP address, usually using IP
None of the preceding five parameters

About Data Source:

If IIS and SQL are on the same server, use the IP address or host name HostName or (local) for IIS installation)
For example:
// Data Source = (Local) ''IIS and SQL are the first choice!
// Data Source = 212.100.1.12
// Data Source = LSS
If SQLserver is on another machine, for example, if you connect to SQLserver on my machine, use the IP address of my machine.
// Data Source = 208.190.21.112 'My SQL server IP Address
CONNect Timeout = connection Timeout, which is an integer. The default value is 30 seconds.
Persist Security Info = true or false

Understand the differences between SQL and ACCESS:

ACCESS is a file-type database, and a database is a specific MDB file. Therefore, the ACCESS connection must provide the specific database path Data Source = 'C: \ www \ mdb \ aaa. mdb'
Server. mappath (aaa. mdb) maps aaa. mdb to c: \ www \ mdb \ aaa. mdb.
SQLserver is the S/C Client/Server method, which is completely different from ACCESS. Therefore, you need to establish a connection between the client and the server to ACCESS the SQL 2000 database. Note that this client is for the SQL database server.
Server scripts are also "Client Applications" for SQLserver ".
SQL databases are also physical. MDF data files, but this is quite different from MDB. SQL MDF is a collection of databases, including many databases (each database has a name, each database has a corresponding owner SchMa), and the access mdb is a file that is a database.
Therefore, to access the SQL database, you must specify the IP address, database account, and password of the server. (Of course, there is no path)
ACCESS only needs to ACCESS the ACCESS file.
Now, go to the Enterprise Manager and create a database (for example, AAA). In the database, create and add a database user and password. Then you can use the following connection string to connect to the database!

// Method 2: (DSN connection)

MM_conn_STRING = "DSN = BBS; UID = SA; PWD = 12345"
Set conn = Server. Createobject ("ADODB. Connection ")
Conn. open MM_conn_STRING
Set rs = SERVER. CreateObject ("ADOBD. recordset ")
SQL = "SELECT * FROM TABLE ORDER BY ID DESC"
RS. open SQL, CONN, 3, 3 // 3, 3 is to modify, delete, add switch!

// Method 3:

MM_conn_STRING_own = "Driver = {SQL}; Server = (local); uid = sa; pwd = 11111; database = infs ;"
Set conn = Server. Createobject ("ADODB. Connection ")
Conn. open MM_conn_STRING_own

// Method 4-this method is used in ACCESS
Strconn = "DRIVER = Microsoft Access Driver (*. mdb); DBQ = "_
& Server. MapPath ("asp. mdb ")
Set conn = server. createobject ("adodb. connection ")
Conn. open strconn

Common functions available for both sqlserver and accessCopy codeThe Code is as follows: <%
Const DataBaseType = 1
If DataBaseType = 0 then
DBPath = "/data/news. mdb" // mdb database path
Else
'If it is a SQL database, Please carefully modify the following database options
DataServer = "jb51" 'database Server IP Address
DataUser = "jb51" 'user name for accessing the database
DataBaseName = "jb51" 'Database Name
DataBasePsw = "www.jb51.net" 'database access password
End if
On Error Resume Next
If DataBaseType = 1 Then
ConnStr = "driver = {SQL Server}; server =" & dataserver & "; UID =" & datauser & "; PWD =" & databasepsw & "; Database =" & databasename
Else
ConnStr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath (DBPath)
End If
Set conn = Server. CreateObject ("ADODB. Connection ")
Conn. open ConnStr
If Err Then Err. Clear: Set conn = Nothing: Response. Write "the database connection fails. Check the database parameter settings in the Conn. asp file. ": Response. End
%>

Connect MSSQL code (additional Judgment code ):Copy codeThe Code is as follows: <%
Dim cn
Set cn = server. createobject ("adodb. connection ")
Cn. connectionstring = "driver = {SQL server}; server = taihang; datebase = taihang; uid = sa; pwd = hacker"
Cn. open
If cn. state = 1 Then
Response. write "the database connection object has been opened"
Else
Response. write "the database connection object is not opened"
End If
Cn. close
Set cn = nothing
%>

Common Methods for connecting to Access:

Copy codeThe Code is as follows: Dim conn, strDataPath, connStr
Set conn = server. createobject ("ADODB. connection") '// defines the database connection object
StrDataPath = server. MapPath ("example. mdb") '// database path string
ConnStr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & strDataPath '// database connection
Conn. open connStr

You can also use:

Copy codeThe Code is as follows: Dim conn, strDataPath, connStr
Set conn = server. createobject ("ADODB. connection") '// defines the database connection object
StrDataPath = server. MapPath ("example. mdb") '// database path string
ConnStr = "driver = {Microsoft Access Driver (*. mdb)}; dbq =" & strDataPath '// database connection
Conn. Open connStr

Note:
Because we want to open Access (. mdb) database, so we need to use the Access ODBC Driver {Microsoft Access Driver (*. mdb)} to access the database. The dbp parameter is used to specify the database file to be opened. Because it must be the full path name, the Server is used in the previous statement. mapPath function.

The following sentence can be used for the sake of conciseness.
Conn. open "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath ("/") & "xxx. mdb"

Cold water comments>
Microsoft. Jet. OLEDB.4.0 interface is generally used directly:
AdoConnection. open "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath ("/") & "xxx. mdb"
Avoid using the Microsoft OLEDB Provider for ODBC Drivers + Microsoft Access Driver (*. mdb) (Access ODBC Driver) two-layer interface:
AdoConnection. open "PROVIDER = MSDASQL.1; Driver = Microsoft Access Driver (*. mdb); DBQ =" & Server. MapPath ("/") & xxx. mdb"

ACCESS database connection code (1)Copy codeThe Code is as follows: <%
Set con = Server. CreateObject ("ADODB. Connection ")
Con. Open "DRIVER = {Microsoft Access Driver (*. mdb)}; DBQ =" & Server. MapPath ("database file path ")
%>

ACCESS database connection code (2)Copy codeThe Code is as follows: <%
Dim conn, cqie
Conn = "provider = microsoft. jet. oledb.4.0; data source =" & server. MapPath ("database file path ")
Set cqie = server. createobject ("adodb. connection ")
Cqie. open conn
%>

Simple Random Functions in aspCopy codeThe Code is as follows: <%
Dim a, B, c
A = 1310: B = 9215
Randomize
C = int (B-a + 1) * rnd +)
%>

Retrieve the Last Logon Time and record the current Logon TimeCopy codeThe Code is as follows: <%
Session ("onetime") = rs ("Last login time ")
Session. timeout = 30
Rs ("Last login time") = now ()
%>

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.