ASP数据库问题

ASP数据库问题,第1张

我来教你做个简单明了的,请按我的步骤一步一步来:

1\在本地站点的任何目录下建立一个ACCESS数据库:

A.数据库名称:user.mdb

b.建立表login

c.字段:

id:自动编号,主健

username:文本类型,用户名

userpasword:文本类型,用户密码

2\在DW2004新建连接数据库文件:conn.asp(文件名你自己定,我们习惯用这个名),其代码如下(连接ACCESS数据库的,最好是纯的ASP代码,不要含有任何HTML脚本):

<%

set conn=Server.CreateObject("ADODB.Connection")

DBPath = Server.MapPath("user.mdb")

conn.Open "provider=microsoft.jet.oledb.4.0data source="&dbpath

%>

3\新建站点首页index.htm 代码:

<!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/htmlcharset=gb2312">

<title>简单登陆页面</title>

<SCRIPT language="JAVASCRIPT">

<!--

function check_Null(){

if (document.form1.username.value==""){

alert("请输入用户名!")

document.form1.username.focus()

return false

}

if (document.form1.userpassword.value==""){

alert("请输入密码!")

document.form1.userpassword.focus()

return false

}

return true

}

// -->

</SCRIPT>

</head>

<body>

<form method="post" action="login.asp" name="form1" onsubmit="javascript:return check_Null()">

用户名:<input name="username" type="text" size="12">

密 码<input name="userpassword" type="password" size="12">

<input name="submit" type="submit" value="登陆">

<input type="reset" name="Submit" value="重新输入">

</form>

</body>

</html>

4\新建登陆检验用户名和密码页login.asp(纯ASP代码):

<!--#include file="conn.asp"--><!--把连接数据库代码的页面包含进来,有用-->

<% dim user,password,rs'声明变量

username=trim(request.form("username"))

userpassword=trim(request.form("userpassword"))

set rs=conn.execute("select username from login where username='"&username&"'") '查询数据库表login中的记录

if rs.eof then '如果数据库中还没有这个用户名,就把这个用户名和密码写进数据库:

conn.execute("insert into login(username,userpassword) values('"&username&"','"&userpassword&"'")

response.write"<script>alert('您这是第一次登陆,请记住登陆名和密码!')window.location='***.asp'</script>" 'd出消息给用户并且转向***.asp页面

else '如果已经有这个用户名,就执行下面SQL语句检查密码正确与否:

set rs=conn.execute("select userpassword from login where userpassword='"&userpassword&"'")

if trim(rs("userpassword"))<>userpassword then

response.write"<script>alert('密码错误,请重新输入密码!')history.go(-1)</script>" 'd出消息提示用户密码错误,返回重新输入密码

else

response.write"<script>alert('登陆成功!')window.location='***.asp'</script>" 'd出登陆成功消息给用户并且转向***.asp页面

end if

end if

'释放数据库资源

rs.close

set rs=nothing

conn.close

set conn=nothing

response.end()

%>

5\把你的数据库和所有页面文件FTP到空间就可以访问了

-----仅供参开-----------------------------

访问数据库资源需要创建连接、打开连接和关闭连接几个 *** 作。这些过程需要多次与数据库交换信息以通过身份验证,比较耗费服务器资

源。ASP.NET中提供了连接池(Connection Pool)改善打开和关闭数据库对性能的影响。系统将用户的数据库连接放在连接池中,需要时取出,关闭时收回连接,等待下一次的连接请求。

连接池的大小是有限的,如果在连接池达到最大限度后仍要求创建连接,必然大大影响性能。因此,在建立数据库连接后只有在真正需要 *** 作时才打开连接,使用完毕后马上关闭,从而尽量减少数据库连接打开的时间,避免出现超出连接限制的情况。

用(推荐)

using(SqlConnection Conn=new SqlConnection(connstr))

{}//不必显示关闭

try{conn.Open()}

catch{}

finally{conn.Close()}

那个,,主要是,,当前装载页面,,在页面中所有的RS...还有连接,,都要关闭...关闭就行了..

但还有一个问题,,conn,,这个连接,,用不着时时关闭...只要是有读数据库的页面,,都要连接的...


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9926732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-03
下一篇 2023-05-03

发表评论

登录后才能评论

评论列表(0条)

保存