主题:ASP中各种数据库连接代码
yaozheng
[专家分:28410] 发布于 2004-04-07 09:33:00
最近看到有不少朋友在问关于数据库连接代码的问题,找了这篇东西给大家看看,希望有帮助
MS Access数据库连接
用DSN连接并且没有用户名和密码:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName"
%>
用DSN连接并且有用户名和密码:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "YourDSNName","username","password"
%>
用实际的数据库绝对路径连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=e:\yanhang\database.mdb"
conn.Open Strconn
%>
用实际的数据库相对路径连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=" & Server.MapPath("/database/yanhang.mdb")
conn.Open Strconn
%>
MS SQL Server数据库连接
用DSN连接:
<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open "DSN=MyDSN;UID=user;PWD=password;DATABASE=databasename"
%>
不用DSN连接:
<%
Set conn = Server.CreateObject("ADODB.Connection")
DSNtemp="DRIVER={SQL Server};SERVER=ServerName;UID=USER;PWD=password;DATABASE=databasename"
conn.open DSNtemp
%>
FoxPro数据库连接
<%
Set Conn = Server.CreateObject("ADODB.connection")
ConnStr= "Driver=Microsoft Visual Foxpro Driver; UID=userID;SourceType=DBC;SourceDB=C:\yanhang\database.dbc"
Conn.Open ConnStr
%>
Oracle数据库连接:
<%
set conn=server.createobject("adodb.connection")
conn.cursorlocation=adUseClient
DSNTemp="Provider=MSDAORA.1;Password=xxxxx;User ID=yanhang;Data Source=xxx.world"
conn.open DSNtemp
%>
回复列表 (共200个回复)
121 楼
fnzxc [专家分:0] 发布于 2006-04-15 11:51:00
把重要内容置顶
122 楼
logzhiwen [专家分:0] 发布于 2006-04-17 18:49:00
小弟在asp ,做了一个留言薄;但是想把内容写进数据库,可是连接不上,求助高手解决:
数据库db1.mdb我建好了
代码如下
louy.asp
<%
if request("b1")<>empty then
name1=request("t1")
tell=request("t2")
mail1=request("t3")
mainpage=request("t4")
word1=request("s1")
if name1<>empty and word1<>empty then
Set conn = Server.CreateObject("ADODB.Connection")
ConnStr = "Provider = microsoft.jet.oledb.4.0;Data Source="&Server.MapPath("db1.mdb")
Conn.Open
myspl="Insert into 留言(姓名,电话,email,主页,留言) values('"&name1&"','"&tell&"','"&maill&"','"&mainpage&"','"&word1&"')"
Response.Cookies("n1")=name1
Response.Cookies("te1")=tell
Response.Cookies("e1")=maill
Response.Cookies("m1")=mainpage
Response.Cookies("w1")=word1
conn.execute(myspl)
response.Write " <font color=yellow>"&name1&",恭喜你,留言以发出!</font> "
else
Response.Write "<center> <font color=red>请你注意'姓名'与'留言'栏必须填写</font></center>"
end if
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>超级留言册</title>
<style type="text/css">
<!--
.style1 {
font-family: "宋体";
font-size: 36px;
}
.style2 {
font-family: "宋体";
font-size: 24px;
}
.style3 {
font-family: "宋体";
font-size: 14px;
}
-->
</style>
</head>
<body bgcolor="cccccc">
<form name="form1" method="post" action="">
<p align="center" class="style2">请你注意'姓名'与'留言'栏必填</p>
<p align="center"><span class="style3">姓 名</span> :
<input type="text" name="t1">
</p>
<p align="center"><span class="style3">电 话</span> :
<input type="text" name="t2">
</p>
<p align="center"><span class="style3">EMAIL</span> :
<input type="text" name="t3">
</p>
<p align="center"><span class="style3">主 页</span>:
<input type="text" name="t4">
</p>
<p align="center" class="style1">填 写 留 言</p>
<p align="center" class="style3">
<textarea name="s1" cols="100" rows="8"></textarea>
</p>
<p align="center" class="style3">
<input name="b1" type="submit" class="style2" value="提 交">
<input name="b2" type="reset" class="style2" id="reset" value="重 置">
</p>
<p align="left" class="style3">
<a href="view3.asp" target="_top">查看留言</a></p>
<!--建立查看留言的超链接,并将target设为'_top'让所连接的view3.asp全屏显示-->
</form>
<p align="left"></p>
</body>
</html>
view3.asp代码
%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body bgcolor="#666666">
<center>
<!--以表格形式显示留言的信息内容-->
<h1><font color="#666666" face="宋体">年的留言内容如下所示</font></h1>
<table border="1">
<tr bgcolor="#CCCCCC">
<!--用“<tr>”>元素定义表格的行-->
<%
Response.Write "<td><center>姓名</center></td>"
Response.Write "<td><center>电话</center></td>"
Response.Write "<td><center>eamil</center></td>"
Response.Write "<td><center>主页</center></td>"
Response.Write "<td><center>留言</center></td>"
%>
</tr>
<%
n1=request.Cookies("n1")
te1=request.Cookies("te1")
e1=request.Cookies("e1")
m1=request.Cookies("m1")
w1=request.Cookies("w1")
Response.Write "<TR bgcolor=#ff99ff><td><br><p><br>"&n1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&te1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&e1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&m1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&w1&"<br><p><br></td>"
%>
</table>
</center>
<p align="left"><a href="louy.asp" target="_top">返回主页</a></p>
</body>
</html>
123 楼
logzhiwen [专家分:0] 发布于 2006-04-17 18:50:00
小弟在asp ,做了一个留言薄;但是想把内容写进数据库,可是连接不上,求助高手解决:
数据库db1.mdb我建好了
代码如下
louy.asp
<%
if request("b1")<>empty then
name1=request("t1")
tell=request("t2")
mail1=request("t3")
mainpage=request("t4")
word1=request("s1")
if name1<>empty and word1<>empty then
Set conn = Server.CreateObject("ADODB.Connection")
ConnStr = "Provider = microsoft.jet.oledb.4.0;Data Source="&Server.MapPath("db1.mdb")
Conn.Open
myspl="Insert into 留言(姓名,电话,email,主页,留言) values('"&name1&"','"&tell&"','"&maill&"','"&mainpage&"','"&word1&"')"
Response.Cookies("n1")=name1
Response.Cookies("te1")=tell
Response.Cookies("e1")=maill
Response.Cookies("m1")=mainpage
Response.Cookies("w1")=word1
conn.execute(myspl)
response.Write " <font color=yellow>"&name1&",恭喜你,留言以发出!</font> "
else
Response.Write "<center> <font color=red>请你注意'姓名'与'留言'栏必须填写</font></center>"
end if
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>超级留言册</title>
<style type="text/css">
<!--
.style1 {
font-family: "宋体";
font-size: 36px;
}
.style2 {
font-family: "宋体";
font-size: 24px;
}
.style3 {
font-family: "宋体";
font-size: 14px;
}
-->
</style>
</head>
<body bgcolor="cccccc">
<form name="form1" method="post" action="">
<p align="center" class="style2">请你注意'姓名'与'留言'栏必填</p>
<p align="center"><span class="style3">姓 名</span> :
<input type="text" name="t1">
</p>
<p align="center"><span class="style3">电 话</span> :
<input type="text" name="t2">
</p>
<p align="center"><span class="style3">EMAIL</span> :
<input type="text" name="t3">
</p>
<p align="center"><span class="style3">主 页</span>:
<input type="text" name="t4">
</p>
<p align="center" class="style1">填 写 留 言</p>
<p align="center" class="style3">
<textarea name="s1" cols="100" rows="8"></textarea>
</p>
<p align="center" class="style3">
<input name="b1" type="submit" class="style2" value="提 交">
<input name="b2" type="reset" class="style2" id="reset" value="重 置">
</p>
<p align="left" class="style3">
<a href="view3.asp" target="_top">查看留言</a></p>
<!--建立查看留言的超链接,并将target设为'_top'让所连接的view3.asp全屏显示-->
</form>
<p align="left"></p>
</body>
</html>
view3.asp代码
%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body bgcolor="#666666">
<center>
<!--以表格形式显示留言的信息内容-->
<h1><font color="#666666" face="宋体">年的留言内容如下所示</font></h1>
<table border="1">
<tr bgcolor="#CCCCCC">
<!--用“<tr>”>元素定义表格的行-->
<%
Response.Write "<td><center>姓名</center></td>"
Response.Write "<td><center>电话</center></td>"
Response.Write "<td><center>eamil</center></td>"
Response.Write "<td><center>主页</center></td>"
Response.Write "<td><center>留言</center></td>"
%>
</tr>
<%
n1=request.Cookies("n1")
te1=request.Cookies("te1")
e1=request.Cookies("e1")
m1=request.Cookies("m1")
w1=request.Cookies("w1")
Response.Write "<TR bgcolor=#ff99ff><td><br><p><br>"&n1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&te1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&e1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&m1&"<br><p><br></td>"
Response.Write "<td><br><p><br>"&w1&"<br><p><br></td>"
%>
</table>
</center>
<p align="left"><a href="louy.asp" target="_top">返回主页</a></p>
</body>
</html>
124 楼
wuqi123321 [专家分:0] 发布于 2006-04-19 19:19:00
各位高手,我用
<%
Set conn = Server.CreateObject("ADODB.Connection")
Strconn="DRIVER={Microsoft Access Driver (*.mdb)}; "
Strconn=Strconn & "DBQ=" & Server.MapPath("/database/yanhang.mdb")
conn.Open Strconn
%>
连接数据库连不上,而用
db=data\data.mdb
on error resume next
set conn=Server.CreateObject("Adodb.Connection")
cnnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="Server.MapPath(db)"
conn.open connstr
却连上了,为什么,是我的机子没有驱动程序吗?
125 楼
wuqi123321 [专家分:0] 发布于 2006-04-19 19:20:00
是
126 楼
kbivs [专家分:0] 发布于 2006-04-20 10:58:00
有没有连接POSTGRESQL的?
127 楼
hanry [专家分:0] 发布于 2006-04-20 22:13:00
我是新手,有许多的东西都不懂,请问上面所说的DNS是什么??
128 楼
hanry [专家分:0] 发布于 2006-04-20 22:29:00
DSN 是什么意思??
129 楼
小の鱼 [专家分:20] 发布于 2006-04-21 16:12:00
不错呀~~!
130 楼
saten [专家分:0] 发布于 2006-04-21 18:52:00
各位老大
我 连是连上了
也可以读的出来
但是写不进去阿
我来回复