在ASP中連接MySQL數據庫有多種方法,以下是常見的兩種方法:
1. 使用ADODB連接對象(ActiveX Data Objects)
```asp
<%
Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;DATABASE=mydatabase;USER=username;PASSWORD=password;OPTION=3;"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM tablename", conn
While Not rs.EOF
Response.Write(rs("columnname") & "
")
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
```
2. 使用MySQL Connector/ODBC驅動程序
```asp
<%
Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "DSN=mydsn;UID=username;PWD=password;"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM tablename", conn
While Not rs.EOF
Response.Write(rs("columnname") & "
")
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
%>
```
請注意,上述代碼中的參數(如服務器地址、數據庫名稱、用戶名、密碼等)需要根據實際情況進行修改。