ASP connection SQL and Access data code (random Functions in ASP) _ Application Tips

Source: Internet
Author: User
Tags dsn mdb database odbc access database
ASP Connection sql
The first method of writing:
Copy Code code as follows:

mm_conn_string = "Driver={sql server};server= (local); uid=sa;pwd=;d Atabase=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 Code code as follows:

DataServer = "jb51" ' Database server IP
Datauser = "jb51" ' Access to database user name
DataBaseName = "jb51" database name
DATABASEPSW = "Www.jb51.net" Access to database password
Set conn = Server.CreateObject ("ADODB. Connection ")
Connstr= "Driver={sql server};server=" "&dataserver&"; Uid= "&datauser&"; Pwd= "&databasepsw&";D atabase= "&databasename
Conn.Open ConnStr
If ERR Then Err.Clear:Set conn = Nothing:Response.Write database connection error, check the database parameter settings in the Conn.asp file. ": Response.End


Using ASP to connect to the MS SQL database, the standard connection, commonly used is the following connection string:
Copy 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 the database provider, the following 1 is the version information, and if not, the current newest feature is fully used
User id=sql account number, Database account number
Password=sql account password, database account password
Initial catalog= Database name (only the name, and access is the specific data file with the path)
Data source, SQL Server name, or its IP, usually with IP source=
The five parameters are indispensable

About Data Source:

If IIS and SQL are the same server, use the IP or host name hostname or (local) that installs IIS
For example:
Data source= (local) ' IIS is the first to choose it with SQL!
Data source=212.100.1.12
Data SOURCE=LSS
If SQL Server is on another machine, such as you connect to SQL Server on my machine, use my machine's IP
Data source=208.190.21.112 ' My SQL Server IP
Connect timeout= connection Timeout, is an integer, the default is 30 seconds, you can not
Persist security info= True or false can be used without

Understand the difference between SQL and access:

Access is a file-type database, a database is a specific MDB file, so access connections need to give specific database path data source= ' C:\www\mdb\aaa.mdb '
and Server.MapPath (Aaa.mdb) is to map Aaa.mdb to C:\www\mdb\aaa.mdb
SQL Server is a S/C client/server approach that is completely different from access, so accessing SQL 2000 databases requires a client-and server-side connection, noting that this client is for SQL database servers
Server-side scripts are also "client applications" for SQL Servers.
The SQL database is also physically a. mdf data file, but this is different from the MDB, and SQL MDF is a collection of databases that contain a number of databases (each of which has a name, each database has a corresponding owner Schma), And the MDB of access is a file is a database.
So access to the SQL database to specify its server IP database account password database name (of course there is no path to say)
Access can be accessed only by accessing the file in Access.
Now it's time to go into Enterprise Manager, build a database (for example, AAA), create a database user and password in the database, and then connect to it with the connection string above!

The second way: (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 modify, delete, add switch!

The Third Way:

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);D bq=" _
& Server.MapPath ("Asp.mdb")
Set conn = Server.CreateObject ("Adodb.connection")
Conn.Open strconn

The functions we use for SQL Server and Access
Copy 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 to database user name
DataBaseName = "jb51" database name
DATABASEPSW = "Www.jb51.net" Access to database password
End If
On Error Resume Next
If DatabaseType = 1 Then
Connstr= "Driver={sql server};server=" "&dataserver&"; Uid= "&datauser&"; Pwd= "&databasepsw&";D atabase= "&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, check the database parameter settings in the Conn.asp file. ": Response.End
%>


Connect MSSQL Code (additional judgment code):
Copy Code code 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 "Database Connection object is open"
Else
Response.Write "Database Connection object not open"
End If
Cn.close
Set cn=nothing
%>


Common methods for connecting to access:

Copy Code code as follows:

Dim Conn,strdatapath,connstr
Set Conn=server.createobject ("Adodb.connection") '//Define database connection objects
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 Code code as follows:

Dim Conn,strdatapath,connstr
Set Conn=server.createobject ("Adodb.connection") '//Define database connection objects
Strdatapath=server. MapPath ("Example.mdb") '//database path string
connstr= "Driver={microsoft Access driver (*.mdb)};d bq=" & Strdatapath '/database connection
Conn. Open ConnStr


Annotations:
Because we want to open an access (. mdb) database, we want to access the database through the ODBC driver {Microsoft access Driver (*.mdb)} in Access, and the DBP parameter is used to specify the database file that you want to open. Because it must be the full path name, we used the Server.MapPath function in the previous statement.

The above commonly used methods are generally for the sake of simplicity also can use the following sentence
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath ("/") & "Xxx.mdb"

Water Cold Opinion >>
Generally direct use of the MICROSOFT.JET.OLEDB.4.0 layer interface:
Adoconnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & Server.MapPath ("/") & "Xxx.mdb"
Avoid using the Microsoft OLE DB Provider for ODBC Drivers + Microsoft Access Driver (*.mdb) (Access ODBC Driver) Two-tier interface:
Adoconnection.open "Provider=msdasql.1;driver=microsoft Access Driver (*.mdb);D bq=" & Server.MapPath ("/") & Xxx.mdb "

Access database Connection code (i)
Copy 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 (II)
Copy 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 functions in ASP
Copy 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 record this time
Copy Code code 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.