asp怎么连接mysql数据库
1、本文是测试性的,可以通过程序直接新建表、创建字段等,但为了简便起见,我们先在万网会员中心,数据库管理里面,创建一个名为Tb_Talking的表,表中新建几个字段,如下图所示

2、通过FTP工具,在空间新建mytest文件夹,在mytest文件夹下,新建conn.asp和test.asp两个文件。

3、其中conn.asp文件夹下的内容为:
<%
Dim Conn,ConnStr,DataServer,DataBase,DataUser,DataPass
DataServer = "xxxxxx" //服务器IP
DataBase = "xxxxxx" //数据库名
DataUser = "xxxxxx" //用户名
DataPass = "xxxxxx" //密码
ConnStr="Provider=SQLOLEDB.1;User ID="&DataUser&";Password="&DataPass&";Initial Catalog="&DataBase&";Data Source="&DataServer&""
Set conn=Server.CreateObject("ADODB.Connection")
conn.ConnectionString=ConnStr
conn.Open
%>
4、//test.asp内容
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
<%Response.Charset="gb2312"%>
<!--#include file="conn.asp"-->
<script language="javascript">
function check()
{
if(document.form1.say_anything.value=="")
{
alert("输入的内容不能为空!");
document.form1.say_anything.focus();
return false;
}
document.form1.ctype.value="add";
document.form1.submit();
}
</script>
<%
currentTime="¤"&year(Now)&"-"&Month(Now)&"-"&day(Now)&" "&Hour(Now)&":"&Minute(Now)&":"&Second(Now)
response.Write currentTime
if trim(request("ctype"))="add" then
session("say_anything")=""
sql="insert into Tb_Talking(Content,Time) values('"&trim(request("say_anything"))&"','"+currentTime+"') "
set rs=conn.execute(sql)
end if
%>
<html>
<head>
<title>test</title>
<link href="admin.css" type="text/css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body style="text-align:center">
<div class="div1">
<table>
<tr>
<form action="" name="form1">
<input type="hidden" name="ctype">
<td>
<textarea name="say_anything" id="say" style="width:450; height:100; overflow:auto"></textarea>
</td>
<td>
<input type="button" name="publish" onClick="check()" style=" height:30px; width:50px;" value="发表">
</form>
</td>
</tr>
</table>
</div>
<div style="margin:0 auto; width:500px">
<%
set rl=server.createobject("adodb.recordset")
sqlres="Select * from Tb_Talking"
rl.open sqlres,Conn,1,3
if not(rl.bof or rl.eof)then
Do While Not rl.Eof
%>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td><font color="#CCCCCC" face="Arial, Helvetica, sans-serif"><%=rl("Content")%></font></td>
<td><font size="-1"><%=rl("Time")%></font></td>
</tr>
</table>
<br>
<br>
<%
rl.MoveNext
Loop
%>
<%End if%>
</div>
</body>
</html>
5、test.asp中包含“<link href="admin.css" type="text/css" rel="stylesheet">”,简洁起见,admin.css的内容就不贴出来了。这一语句去掉也可以。查询和插入数据的js语句如下图所示:


6、最后在地址栏上输入www.域名/mytest/test.asp,结果如图所示
