您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > asp连接数据库代码实例
连接数据库代码实例1,连接数据库代码文件名称conn.asp所有访问数据库的文件都调用此文件!--#includefile=\Conn.asp\--%db=\data/data.mdb\\'数据库存放目录onerrorresumenextsetconn=server.createobject(\adodb.connection\)conn.open\driver={microsoftaccessdriver(*.mdb)};dbq=\&server.mappath(db)iferrthenerr.clearsetconn=Nothingresponse.write\数据库连接出错,请检查conn.asp中的连接字符串。\response.endendiffunctionCloseDBConn.ClosesetConn=NothingEndFunction%%dimbadwordbadword=\\'|and|select|update|chr|delete|%20from|;|insert|mid|master.|set|chr(37)|=\ifrequest.QueryString\\thenchk=split(badword,\|\)foreachquery_nameinrequest.querystringfori=0toubound(chk)ifinstr(lcase(request.querystring(query_name)),chk(i))0thenresponse.write\scriptlanguage=javascriptalert(\'传参错误!参数\&query_name&\的值中包含非法字符串!\\n\\n\');location=\'\&request.ServerVariables(\HTTP_REFERER\)&\\'/Script\response.endendifnextnextendif%----------------------------------------------2。增加纪录%ifrequest(\action\)=\add\thenname=request.form(\name\)content=request.form(\content\)setrs=server.createobject(\adodb.recordset\)sql=\select*frombiao\rs.opensql,conn,3,2rs.addnewrs(\name\)=nameifcontent\\thenrs(\content\)=contentelsers(\content\)=nullendifrs(\date\)=date()rs.updaters.closesetrs=nothingresponse.write\scriptlanguage=javascriptalert(\'添加成功!\');location.href(\'index.asp\');/script\endif%--------------------------------------3.显示记录%setrs=server.createobject(\adodb.recordset\)sql=\select*frombiaoorderbyiddesc\\'sql=\selecttop10*frombiaoorderbyiddesc\rs.opensql,conn,1,1rs.pagesize=15\'-------设置每页显示的记录数dimpagepage=request(\page\)ifpage\\andIsNumeric(page)thenpage=clng(page)elsepage=1endifn=rs.pagecountifpagenthenpage=clng(n)endififrs.eofthenresponse.write\fontcolor=#FF0000暂没有信息!/font\\'response.endelsers.absolutepage=pageendifi=0dowhilenotrs.eofandirs.pagesize\'dowhilenotrs.eof%--------如果是每行显示n个纪录开始----------------------------%dowhilenotrs.eofandirs.pagesize\'dowhilenotrs.eofifimod5=0then\'--------设置每行显示的个数response.write\tr\endif%--------如果是每行显示n个纪录结束-----------------------------%=rs(\id\)%%rs.movenexti=i+1loop%%response.write(\共\&rs.recordcount&\条信息 \)ifpage1thenresponse.write(\ahref=?page=1title=\'首页\'首页/a \)elseresponse.write(\首页 \)endififpage1thenresponse.write(\ahref=?page=\&page-1&\title=\'上一页\'上一页/a \)elseresponse.write(\上一页 \)endififpagenthenresponse.write(\ahref=?page=\&page+1&\title=\'下一页\'下一页/a \)elseresponse.write(\下一页 \)endififpagenthenresponse.write(\ahref=?page=\&n&\title=\'尾页\'尾页/a \)elseresponse.write(\尾页 \)endifresponse.write(\ 当前页:\&page&\/\&n&\\)%转到:selectname=\select\onChange=\'javascript:window.open(this.options[this.selectedIndex].value,\_top\)\'%forp=1tors.pagecount%optionvalue=\?page=%=p%\%ifpage=pthenresponse.write\selected\endif%第%=p%页/option%next%----------------------------------------------4。更新纪录,删除纪录,删除所有记录%ifrequest(\action\)=\manage\thencallmanage()endififrequest(\action\)=\edit\thenid=request(\id\)setrs=server.createobject(\adodb.recordset\)sql=\select*frombiaowhereid=\&id&\\rs.opensql,conn,1,1calledit()endififrequest(\action\)=\del\thenconn.execute(\delete*frombiaowhereid=\&request(\id\)&\\)conn.closeresponse.write\scriptlanguage=\'javascript\'alert(\'删除成功!\');location.href(\'?action=manage\');/script\endififrequest(\action\)=\delall\thenconn.execute(\delete*frombiao\)conn.closeresponse.write\scriptlanguage=\'javascript\'alert(\'所有信息已成功删除!\');location.href(\'?action=manage\');/script\endififrequest(\action\)=\saveedit\thenname=request.form(\name\)hits=request.form(\hits\)content=request.form(\content\)setrs=server.createobject(\adodb.recordset\)sql=\select*frombiaowhereid=\&request(\id\)&\\rs.opensql,conn,3,2rs(\name\)=namers(\content\)=contentrs(\hits\)=hitsrs.updateconn.closesetrs=nothingresponse.write\scriptlanguage=javascriptalert(\'编辑成功!\');location.href(\'?id=\&request(\id\)&\&action=edit\');/script\endif%---------------------------------5。查询纪录formname=form1method=postaction=search.aspinputname=keywordtype=textid=keywordsize=25selectname=selectsize=1optionvalue=nameselected名称/optionoptionvalue=content说明/optionoptionvalue=idid/option/selectinputtype=submitname=Submitvalue=查询/form------search.asp---------------%ifrequest(keyword)andrequest(select)thensql=select*frombiaowhere&request(select)&like'%&request(keyword)&%'elseifrequest(keyword)andrequest(select)=allthensql=select*frombiaowherenamelike'%&request(keyword)&%'oridlike'%&request(keyword)&%'orcontentlike'%&request(keyword)&%'elseresponse.redirect(index.asp)endifsetrs=server.createobject(adodb.recordset)rs.opensql,conn,1,1rs.pagesize=15'-------设置每页显示的记录数dimpagepage=request(page)ifpageandIsNumeric(page)thenpage=clng(page)elsepage=1endifn=rs.pagecountifpagenthenpage=clng(n)endififrs.eofthenresponse.writefontcolor=#FF0000查询的信息不存在或者已经删除!/font'response.endelsers.absolutepage=pageendifi=0dowhilenotrs.eofandirs.pagesize'dowhilenotrs.eof%%=rs(i
本文标题:asp连接数据库代码实例
链接地址:https://www.777doc.com/doc-4477095 .html