虽然是多种方法,其实归根结底都是大同小异,只是书写上的差异而已;但正是这些书写格式也仍然是我们不容忽视的。
1、基本连接
strconnection="driver={mysql odbc 3.51
driver};server=localhost;uid=root;password=luo;database=gaorui;stmt=set
names gb2312"
set conn = server.createobject("adodb.connection")
conn.open strconnection
sql = "select * from user"
set rs = conn.execute(sql)
%>
2、定义变量连接
dim myHost,myDB,myUID,myPWD
myHost = "localhost"
myDB = "gaorui"
myUID = "root"
myPWD = "luo"
myChareSet = "gb2312"
strconnection="driver={mysql odbc 3.51 driver};server=" &
myHost & ";database=" & myDB & ";user name=" &
myUID & ";password=" & myPWD
set conn = server.createobject("adodb.connection")
conn.open strconnection
conn.execute("set names '" & myChareSet & "'")
sql = "select * from user"
set rs = conn.execute(sql)
%>
3、最简单的连接
set conn = server.createobject("adodb.connection")
Conn.Open "driver={MySQL ODBC 3.51
Driver};server=127.0.0.1;port=3306;database=gaorui;user=root;password=luo;stmt=set
names gbk"
sql = "select * from user"
set rs = conn.execute(sql)
%>
4、变量连接字段输出
Dim Connection
Dim ConnectionString
Dim Recordset
Dim SQL
SQL = "SELECT * FROM user"
ConnString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;
DATABASE=gaorui;UID=root;PASSWORD=luo; OPTION=3;stmt=set names
gbk"
Set Connection = Server.CreateObject("ADODB.Connection")
Set Recordset = Server.CreateObject("ADODB.Recordset")
Connection.Open ConnString
Recordset.Open SQL,Connection
If Recordset.EOF Then
Response.Write("No records returned.")
Else
Do While NOT Recordset.Eof
Response.write Recordset("username")
Response.write " "
Response.write Recordset("password")
Response.write "
"
Recordset.MoveNext
Loop
End If
Recordset.Close
Set Recordset=nothing
Connection.Close
Set Connection=nothing
%>
5、另类书写方式连接
Set Conn = Server.CreateObject("ADODB.Connection")
conn.ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};"_
& "SERVER=localhost;"_
& " DATABASE=gaorui;"_
& "UID=root;PWD=luo; OPTION=3;stmt=set names gbk"
conn.Open
sql = "select * from user"
set rs = conn.execute(sql)
%>