asp connection SQL and Access data code, random functions in asp, friends in need can refer to asp connection sql
The first way to write:
Copy the code code 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
Commonly used function codes:
Copy the code code as follows:
DataServer = jb51 'Database server IP
DataUser = jb51 'Access database user name
DataBaseName = jb51 'Database name
DataBasePsw = www.vevb.com 'Access database 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 Database connection error, please check the database parameter settings in the Conn.asp file. :Response.End
Use ASP to connect to MS SQL database. Standard connection. The most commonly used connection string is the following:
Copy the code code 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&
describe:
Provider=SQLOLEDB.1; database provider, the 1 behind it indicates version information, if not, the latest version of the function will be fully used.
User ID=SQL account, database account
Password=SQL account password, database account password
Initial Catalog=Database name (only the name, while ACCESS is the specific data file with path)
Data Source=data source, SQL server name or its IP, usually IP
The above five parameters are indispensable
About Data Source:
If IIS and SQL are on the same server, use the IP or host name where IIS is installed HostName or (local)
For example:
//Data Source=(Local) '' IIS and SQL are the first choices!
//Data Source=212.100.1.12
//Data Source=LSS
If the SQLserver is on another machine, for example, if you connect to the SQLserver on my machine, use the IP of my machine.
//Data Source=208.190.21.112 'My SQL server IP
CONNect Timeout=Connection timeout, it is an integer, the default is 30 seconds, it can not be used
Persist Security Info= true or false can be omitted
Understand the difference between SQL and ACCESS:
ACCESS is a file-type database. A database is a specific MDB file, so the connection to ACCESS needs to give a 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 a S/C client/server mode, which is completely different from ACCESS. Therefore, accessing the SQL 2000 database requires establishing a connection between the client and the server. Note that this client is for the SQL database server.
Server-side scripts appear to SQLserver as client-side applications.
The SQL database is also physically an .MDF data file, but this is completely different from the MDB. The SQL MDF can be said to be a collection of databases, which includes many databases (each database in it has a name, and each database has a name. Each database has a corresponding owner (SchMa), and ACCESS's MDB is a file that is a database.
Therefore, to access the SQL database, you need to specify its server IP, database account, password, and database name (of course there is no path at all)
To access ACCESS, you only need to access the ACCESS file.
What you need to do now is to enter the Enterprise Manager, create a database (for example, AAA), create and add the database user and password in the database, and then you can use the above connection string to connect!
//Second way of writing: (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 the switch for modifying, deleting and adding!
//Third way of writing:
MM_conn_STRING_own = Driver={SQL Server};server=(local);uid=sa;pwd=11111;database=infs;
Set conn = Server.Createobject(ADODB.Connection)
conn.open MM_conn_STRING_own
//Fourth - 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
Functions that can be used in our commonly used sqlserver and access
Copy the code code 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
DataUser = jb51 'Access database user name
DataBaseName = jb51 'Database name
DataBasePsw = www.vevb.com 'Access database 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 Database connection error, please check the database parameter settings in the Conn.asp file. :Response.End
%>
Connection MSSQL code (additional judgment code):
Copy the code code as follows:
<%
dim c
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 database connection object is opened
Else
response.write database connection object is not opened
endIf
cn.close
Set cn=nothing
%>
Common methods for connecting to Access:
Copy the code code as follows:
Dim conn,strDataPath,connStr
set conn=server.createobject(ADODB.connection) '//Define 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
Also available:
Copy the code code as follows:
Dim conn,strDataPath,connStr
set conn=server.createobject(ADODB.connection) '//Define database connection object
strDataPath=server.MapPath(example.mdb) '//Database path string
connStr=driver={Microsoft Access Driver (*.mdb)};dbq= & strDataPath '//Database connection
conn.Open connStr
annotation:
Because we want to open an Access (.mdb) database, we need to access the database through Access's ODBC driver {Microsoft Access Driver (*.mdb)}. The dbp parameter is used to specify the database file we want to open. , because it must be a full path name, so we used the Server.MapPath function in the previous statement.
Among the above commonly used methods, the following sentence can also be used for the sake of simplicity.
conn.open Provider=Microsoft.Jet.OLEDB.4.0;Data Source= & Server.MapPath (/) & xxx.mdb
Shuihan’s opinion >>
Generally, the Microsoft.Jet.OLEDB.4.0 first-level interface is 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's 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 the code code 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 the code code 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 function in asp
Copy the code code as follows:
<%
dim a,b,c
a=1310:b=9215
randomize
c=int((b-a+1)*rnd+a)
%>
Take out the last login time and record the current login time
Copy the code code as follows:
<%
session(onetime)=rs(last login time)
session.timeout=30
rs(last login time)=now()
%>