类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
在1个页面中如果要2次(以上)调用数据库 该怎么做?
比如:
我的数据库是data.mdb
表:article(id,content,title,time) , notice(id,title,content,time)
conn.asp
<%
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("data.mdb")
conn.open connstr
If Err Then
err.Clear
Set Conn = Nothing
Response.Write "数据库连接出错,请检查连接字串。"
Response.End
End If
%>
<% sub notice()
sql="select top 5 * from notice order by id desc"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
Do While not rs.eof
%>
<TR height="25">
<TD width=99%><% =rs("title")%>   <% =month(rs("time")) %>-<% =day(rs("time")) %></td>
</TR>
<%
rs.movenext
if rs.eof then exit do
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
end sub
%>
<%
sub article_pop()
sql="select top 5 * from article order by id desc"
Set rs=Server.CreateObject("ADODB.Recordset")
rs.open sql,conn,1,1
Do While not rs.eof
%>
<TR height="20">
<TD width="80%"><% =rs("title")%></td>
<td width="20%"><% =month(rs("time")) %>-<% =day(rs("time")) %></td>
</TR>
<%
rs.movenext
if rs.eof then exit do
loop
rs.close
set rs=nothing
conn.close
set conn=nothing
end sub
%>
display.asp页面
<html>
<body>
<table width=400 border=1>
<tbody>
<% call notice()% > 第一次
</tbody>
</table>
<table width=400 border=1>
<tbody>
<% call article_pop()%> 第2次
</tbody>
</table>
</body>
</html>
为什么只显示第一次调用的 不显示第二次调用的数据呢
请高手们给小弟讲讲 谢谢!!~~~~~
(我是初学asp:) 分数不多了 不要介意哦:))
网友回答:
调用没错de
如这样的可以:
<%
sub notice()
Response.Write("Call 1")
End sub
%>
<%
sub article_pop()
Response.Write("Call 2")
End sub
%>
<html>
<body>
<table width=400 border=1>
<tbody>
<% Call notice() %>
</tbody>
</table>
<table width=400 border=1>
<tbody>
<% Call article_pop() %>
</tbody>
</table>
</body>
</html>
第二rs改rs1试试 ||| 寒~~
我曾经一个用rs,另一个用conn.execute(……)实现的,你可以试试,因为我那个当时不需要两次rs调用,所以没有碰到你的情况。
或者设置两个不同的ADODB.Connection试一下
设置两次rs是没错的
用rs rs1也是对的
up
为什么把conn给关了,关了 谁连接数据库??
在过程里都关闭了CONN 掉用第一个过程结束后conn.close而且set conn=nothing
第2个过程就没有conn对象可用了 当然不行,写一个专门的过程来关闭和释放conn,在程序结束时调用一下这个过程