想要asp能连接mysql数据库需要安装MySQL ODBC 3.51 驱动 https://www.uoften.com/softs/19910.html
我们先看下面这段代码

复制代码 代码如下:

set conn = server.createobject("adodb.connection")
Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=127.0.0.1;DATABASE=Shops;USER=root;PASSWORD=xxx;"

上面这代码是核心部分了。

SERVER 连接服务器如127.0.0.1
DATABASE 要选择的数据库 shops
USER 服务器登录用户名
PASSWORD 服务器登录密码

好了下面来看实例
复制代码 代码如下:

<%
'测试读取MySql数据库的内容
strconnection="driver={mysql odbc 3.51 driver};database=weste_net;server=localhost;uid=root;password="
'无需配置dsn
set adodataconn = server.createobject("adodb.connection")
adodataconn.open strconnection
strquery = "select * from News"
set rs = adodataconn.execute(strquery)
if not rs.bof then
%>
<table>
<tr>
<td<b>序列号</b></td>
<td><b>标题</b></td>
</tr>
<%
do while not rs.eof
%>
<tr>
<td><%=rs("News_id")%></td>
<td><%=rs("News_Title")%></td>
</tr>
<%
rs.movenext
loop
%>
</table>
<%
else
response.write("无数据.")
end if
rs.close
adodataconn.close
set adodataconn = nothing
set rsemaildata = nothing
%>


上面我没设置数据库编码如果有中文乱码可以尝试

'设置客户端字符编码
复制代码 代码如下:

conn.execute("set names '" & myChareSet & "'")

来解决
如果要使用3306之外的端口的话我们就需要安装Mysql 的ODBC数据库驱动,安装之后

复制代码 代码如下:

Conn.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=127.0.0.1;PORT=3333;DATABASE=Shops;USER=root;PASSWORD=xxx;"

是没有问题的。

点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部