🤖 Ai回答: Here's a step-by-step guide to connecting ASP (Classic) with MySQL, including code examples and key considerations:
1、Prerequisites
IIS Server with ASP enabled
MySQL Server (installed and running)
MySQL ODBC Driver (installed on IIS server)
Download: [MySQL Connector/ODBC](https://dev.mysql.com/downloads/connector/odbc/)
Database Setup:
sql
CREATE DATABASE myapp;
USE myapp;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
);
2、Connection String (DSN-less)
Use this in your ASP files:
asp
<%
Dim connStr
connStr = Driver={MySQL ODBC 8.0 Unicode Driver}; & _
Server=localhost; & _
Database=myapp; & _
User=root; & _
Password=yourpassword; & _
Option=3;
%
Replace `yourpassword` with your MySQL password
Adjust driver version if needed (e.g., `MySQL ODBC 5.3 Driver`)
3、Database Operations
A、Connect and Fetch Data
asp
<%
Set conn = Server.CreateObject(ADODB.Connection)
conn.Open connStr
Set rs = Server.CreateObject(ADODB.Recordset)
rs.Open SELECT * FROM users, conn
Do While Not rs.EOF
Response.Write ID: & rs(id) & Name: & rs(name) & <br
rs.MoveNext
Loop
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%
B、Insert Data
asp
<%
Set conn = Server.CreateObject(ADODB.Connection)
conn.Open connStr
name = John Doe
email = john@65.hk
sql = INSERT INTO users (name, email) VALUES (' & name & ', ' & email & ')
conn.Execute(sql)
conn.Close
Set conn = Nothing
%
C、Parameterized Query (Prevent SQL Injection)
asp
<%
Set conn = Server.CreateObject(ADODB.Connection)
conn.Open connStr
4、Error Handling
asp
<%
On Error Resume Next
Set conn = Server.CreateObject(ADODB.Connection)
conn.Open connStr
If Err.Number < 0 Then
Response.Write Connection Error: & Err.Description
Response.End
End If
' ..、your code ...
conn.Close
Set conn = Nothing
%
5、Key Considerations
1、Security:
Always use parameterized queries (as shown in 3C) to prevent SQL injection
Store credentials in `web.config` (if possible) or outside web root
Use least-privilege MySQL accounts
2、Performance:
Close connections immediately after use
Use `ADODB.Command` for repeated queries
Limit data with `SELECT` clauses
3、Driver Issues:
32-bit vs 64-bit driver must match IIS application pool
Test connection string with [ODBC Data Source Administrator](https://docs.microsoft.com/en-us/sql/odbc/admin/odbc-data-source-administrator)
4、Character Encoding:
Add `charset=utf8` to connection string if needed:
asp
connStr = ...;charset=utf8;
6、Troubleshooting
Error: `[Microsoft][ODBC Driver Manager] Data source name not found`
Install correct ODBC driver architecture (32/64-bit)
Verify driver name matches connection string
Error: `Access denied for user`
Check MySQL username/password
Ensure host permissions (`GRANT ALL ON myapp.* TO 'user'@'iis_ip'`)
Blank Pages:
Enable ASP errors in IIS:
asp
<% On Error GoTo 0 %
Check browser console for errors
Alternative: MySQL Connector/NET (for ASP.NET)
If using ASP.NET instead of Classic ASP:
1、Install `MySql.Data` NuGet package
2、CExample:
csharp
using MySql.Data.MySqlClient;
string connStr = server=localhost;database=myapp;uid=root;pwd=yourpassword;;
using (MySqlConnection conn = new MySqlConnection(connStr))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand(SELECT * FROM users, conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Response.Write($ID: {reader[id]} Name: {reader[name]}<br);
}
}
}
For Classic ASP, the ODBC approach remains the standard method、Always prioritize security with parameterized queries and proper connection management.