| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 768 人关注过本帖
标题:动态更新数据库的问题??
只看楼主 加入收藏
无根泉
Rank: 2
等 级:新手上路
威 望:4
帖 子:853
专家分:0
注 册:2004-11-4
收藏
 问题点数:0 回复次数:5 
动态更新数据库的问题??

要实现的效果,通过页面修改删除sql server数据库中的数据! 以下是所有的代码,问题是如何修改更新?? login.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>无标题文档</title> <style type="text/css"> <!-- body { background-color: #DEE3ED; } input{font-size:9pt; border:1px solid #888888; background-color:#eeeeee;} table{ font-size:9pt; word-break:break-all; } --> </style> </head>

<body> <%bt=request.Form("bt") if bt<>"" then session("server")=request.Form("server") session("uid")=request.Form("uid") session("pwd")=request.Form("pwd") database=request.Form("database") if database="" then session("database")="master" else session("database")=database end if response.Redirect "index.asp" end if %> <form name="form1" method="post" action=""> <table width="394" height="100" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#000066"> <tr bgcolor="#B0BDD3"> <td width="136"><div align="center">地址</div></td> <td width="464"><input name="server" type="text" id="server" size="40"></td> </tr> <tr bgcolor="#B0BDD3"> <td><div align="center">帐号</div></td> <td><input name="uid" type="text" id="uid" size="40"></td> </tr> <tr bgcolor="#B0BDD3"> <td><div align="center">密码</div></td> <td><input name="pwd" type="text" id="pwd" size="40"></td> </tr> <tr bgcolor="#B0BDD3"> <td><div align="center">数据库</div></td> <td><input name="database" type="text" id="database" size="40"></td> </tr> <tr bgcolor="#B0BDD3"> <td>&nbsp;</td> <td height="20"><input name="bt" type="submit" id="bt" value="提交"> <input type="reset" name="Submit2" value="重置"></td> </tr> </table> </form> </body> </html> conn.asp <% 'response.Write session("server")&session("uid") response.Buffer=true if session("server")="" or session("uid")="" then response.Redirect "login.asp" else set conn=server.CreateObject("adodb.connection") if session("pwd")<>"" then constr="driver={sql server};server="&session("server")&";uid="&session("uid")&";pwd="&session("pwd")&";database="&session("database")&";" 'response.Write constr else constr="driver={sql server};server="&session("server")&";uid="&session("uid")&";database="&session("database")&";" 'response.Write constr end if conn.open constr 'response.Redirect "index.asp" 'response.Write "<script>window.location='index.asp';</script>" 'end if end if%> <style type="text/css"> <!-- body { background-color: #DEE3ED; } input{font-size:9pt; border:1px solid #888888; background-color:#eeeeee;} table{ font-size:9pt; word-break:break-all; } --> </style> left.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="conn.asp"--><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>无标题文档</title> </head>

<body> <% set rs=server.CreateObject("adodb.recordset") Set rs=conn.OpenSchema(20) %> <table width="96%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <td height="20" bgcolor="#DEE3ED"><strong>选择数据表</strong></td> </tr> <% do while not rs.eof if rs("TABLE_TYPE")="TABLE" then%> <tr> <td bgcolor="#DEE3ED"><a href="main.asp?table=<%= rs("TABLE_NAME") %>" target="mainframe"><%= rs("TABLE_NAME") %></a>&nbsp;</td> </tr> <% end if rs.movenext loop %> <tr> <td height="20" bgcolor="#DEE3ED"><a href="out.asp" target="mainframe">退出系统</a></td> </tr> </table> <!--table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <td height="20" bgcolor="#DEE3ED">表名</td> </tr> <% 'do while not rs.eof 'and rs("TABLE_TYPE")="TABLE" %> <tr> <td height="20" bgcolor="#DEE3ED"> <% 'If rs("TABLE_TYPE")="TABLE" and left(rs("TABLE_NAME"),4)<>"MSys" Then 'response.Write rs("TABLE_TYPE")&rs("TABLE_NAME") 'end if %></td> </tr> <% 'rs.movenext 'loop 'rs.close %> </table> </body> </html--> main.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="conn.asp"--> <% table=request.QueryString("table") set rs=server.CreateObject("adodb.recordset") sql="select * from "&table&"" rs.open sql,conn,3,2 %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>信息记录</title> </head>

<body> <table width="100%" height="20" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <td bgcolor="#DEE3ED">当前的操作表<%= table %>&nbsp;</td> </tr> </table> <br> <form name="form1" > <table width="100%" height="40" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <% for i=0 to rs.fields.count-1 %> <td bgcolor="#DEE3ED"><%= rs.fields(i).name %>&nbsp;</td> <% next %> <td bgcolor="#DEE3ED">操作</td> </tr> <% do while not rs.eof %> <tr> <% for i=0 to rs.fields.count-1 %> <td bgcolor="#DEE3ED"><% If rs.fields(i).type="201" and len(rs.fields(i).value)>50 Then %> <textarea name="textarea" cols="50" rows="6" style="border:1px solid #888888;overflow:auto;"><%= rs.fields(i).value%></textarea> <% Else %> <%= rs.fields(i).value%><% End If %></td> <% next %> <td bgcolor="#DEE3ED"><a href="edit.asp?action=edit&table=<%=table%>&<%=rs.fields(0).name%>=<%=rs(0)%>">修改</a>|<a href="edit.asp?action=del&table=<%=table%>&id=<%=rs(0)%>">删除</a></td> </tr> <% rs.movenext loop %> </table> </form> </body> </html> edit.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="conn.asp"--> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>修改</title> <% for each x in request.QueryString if x<>"action" and x<>"table" then set rs=server.CreateObject("adodb.recordset") sql="select * from "&request.QueryString("table")&" where "&x&"='"&request.QueryString(x)&"'" rs.open sql,conn,3,2

%>

<% bt=request.form("bt") if bt<>"" then y=0 for each c in request.Form if c<>"提交" then response.Write c&"="&request.Form(c)&"<br>" end if next end if %> <form name="form1" method="post" action=""> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000077"> <% for each b in rs.fields %> <tr bgcolor="#DEE3EF"> <td width="19%" height="20"><div align="center"><%= b.name %>&nbsp;</div></td> <td width="81%"><input name="<%=b.name%>" type="text" id="<%=b.name%>" value="<%=b.value%>" size="60"></td> </tr> <% next %> <tr bgcolor="#DEE3EF"> <td height="20">&nbsp;</td> <td><input name="bt" type="submit" id="bt" value="提交"> <input type="reset" name="Submit2" value="重置"></td> </tr> </table> </form> <%

end if next 'case "edit" %> 问题就出在edit.asp中, 我不知道那个sql应该怎么写!

搜索更多相关主题的帖子: 数据库 动态 
2005-04-13 10:19
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
在Main.asp里,你采用了表中第一个字段做为主键来显示记录内容,第一个字段不是主键怎么办?


[此贴子已经被作者于2005-4-13 13:13:05编辑过]



天津网站建设 http://www./
2005-04-13 12:15
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 

我帮你改了一下,可以编辑修改了,前提是第一个字段是主键(不修改) 打包下载:

eOkYZR31.rar (4.8 KB) 动态更新数据库的问题??

以下对修改处做说明:(红色部分) conn.asp <% '错误处理 on error resume next

'response.Write session("server")&session("uid") response.Buffer=true if session("server")="" or session("uid")="" then 'response.Redirect "login.asp" response.write "<script>top.location.href='login.asp'</script>" response.end else

set conn=server.CreateObject("adodb.connection") if session("pwd")<>"" then constr="driver={sql server};server="&session("server")&";uid="&session("uid")&";pwd="&session("pwd")&";database="&session("database")&";" 'response.Write constr else constr="driver={sql server};server="&session("server")&";uid="&session("uid")&";database="&session("database")&";" 'response.Write constr end if conn.open constr 'response.Redirect "index.asp" 'response.Write "<script>window.location='index.asp';</script>" 'end if '此处是连接数据库出错后跳回登录页,是主框架页跳,而不是原来写的内部页。 if err then response.write "<script>alert('" & replace(err.Description,"'","\'") & "');top.location.href='login.asp';</script>" response.end end if

end if%> <style type="text/css"> <!-- body { background-color: #DEE3ED; } input{font-size:9pt; border:1px solid #888888; background-color:#eeeeee;} table{ font-size:9pt; word-break:break-all; } --> </style> main.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="conn.asp"-->

<% dim table,page,num,sql,pagecount table=request.QueryString("table") page=request.QueryString("page") '分页号

if page<>"" then if isNumeric(page) then page=CLng(page) else page=1 end if else page=1 end if if table="" then response.write "请选择要操作的表" else set rs=server.CreateObject("adodb.recordset") sql="select * from [" & table & "]" '表名用[]隔开,避免与保留字冲突 rs.open sql,conn,3,2 %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>信息记录</title> </head>

<body> <table width="100%" height="20" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <td bgcolor="#DEE3ED">当前的操作表<%= table %>&nbsp;</td> </tr> </table> <br> <% if rs.eof then response.write "此表暂无数据" rs.close set rs=nothing conn.close set conn=nothing response.end end if %> <form name="form1" > <table width="100%" height="40" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <% for i=0 to rs.fields.count-1 %> <td bgcolor="#DEE3ED"><%= rs.fields(i).name %>&nbsp;</td> <% next %> <td bgcolor="#DEE3ED">操作</td> </tr> <% rs.pagesize=30 pagecount=rs.RecordCount /rs.pagesize if rs.recordcount mod rs.PageSize=0 then pagecount=rs.recordcount\rs.PageSize else pagecount=rs.recordcount\rs.PageSize+1 end if rs.absolutepage=page

do while not rs.eof and num<rs.pagesize %> <tr> <% for i=0 to rs.fields.count-1 %> <td bgcolor="#DEE3ED"><% If rs.fields(i).type="201" and len(rs.fields(i).value)>50 Then %> <textarea name="textarea" cols="50" rows="6" style="border:1px solid #888888;overflow:auto;"><%= rs.fields(i).value%></textarea> <% Else %> <%= rs.fields(i).value%><% End If %></td> <% next %> <td bgcolor="#DEE3ED"><a href="edit.asp?action=edit&table=<%=table%>&<%=rs.fields(0).name%>=<%=rs(0)%>">修改</a>|<a href="edit.asp?action=del&table=<%=table%>&id=<%=rs(0)%>">删除</a></td> </tr> <% num=num+1 rs.movenext loop %> </table> </form> <table width="100%" height="20" border="0" cellpadding="0" cellspacing="1" bgcolor="#000066"> <tr> <td bgcolor="#DEE3ED"> 共 <font color=red><%=pagecount%></font> 页 第 <font color=red><%=page%></font> 页 </td> <td bgcolor="#DEE3ED" align="right"> <% if page>1 then response.write "<a href='main.asp?table=" & table & "&page=" & page-1 & "'>上一页</a> " end if

if page<pagecount then response.write "<a href='main.asp?table=" & table & "&page=" & page+1 & "'>下一页</a>" end if %> </td> </tr> </table> </body> </html>

<% rs.close set rs=nothing conn.close set conn=nothing

End If%> edit.asp <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <!--#include file="conn.asp"--> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>修改/删除</title> <% for each x in request.QueryString if x<>"action" and x<>"table" then set rs=server.CreateObject("adodb.recordset") sql="select * from "&request.QueryString("table")&" where "&x&"='"&request.QueryString(x)&"'" rs.open sql,conn,3,2

%>

<% bt=request.form("bt") if bt<>"" then y=0 for each c in request.Form if c<>"bt" and c<>"SearchKey" then 'response.Write c&"="&request.Form(c)&"<br>" if c<>x then if strValue<>"" then strValue=strValue & "," strValue=strValue & c & "='" & Request.Form(c) & "'" end if end if next end if

%> <form name="form1" method="post" action=""> <!--加入一hidden表单对象,用于保存主键及其值--> <input type="hidden" name="SearchKey" value="<%=x & "='" & request.QueryString(x) & "'"%>"> <table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#000077"> <% for each b in rs.fields %> <tr bgcolor="#DEE3EF"> <td width="19%" height="20"><div align="center"><%= b.name %>&nbsp;</div></td> <td width="81%"><input name="<%=b.name%>" type="text" id="<%=b.name%>" value="<%=b.value%>" size="60"></td> </tr> <% next %> <tr bgcolor="#DEE3EF"> <td height="20">&nbsp;</td> <td><input name="bt" type="submit" id="bt" value="提交"> <input type="reset" name="Submit2" value="重置"></td> </tr> </table> </form> <% end if next

if request.form("bt")<>"" then if request.querystring("action")="del" then '删除记录 sql="delete from [" & request.querystring("table") & "] where " & request.form("SearchKey") conn.execute sql response.Redirect "main.asp?table=" & request.querystring("table") elseif request.querystring("action")="edit" then  '修改数据 sql="update [" & request.querystring("table") & "] set " & strValue & " where " & request.form("SearchKey") conn.execute sql response.Redirect "main.asp?table=" & request.querystring("table") end if end if

rs.close set rs=nothing conn.close set conn=nothing %> 另外,你没提供index.asp,out.asp,这补上 index.asp <frameset cols="200,*"> <frame src="left.asp" name="leftframe"> <frame src="main.asp" name="mainframe"> </frameset> out.asp <% session.Abandon  'Session注销 response.write "<script>top.location.href='login.asp'</script>" '框架跳回登录页 response.end %>

[此贴子已经被作者于2005-4-13 14:12:13编辑过]


天津网站建设 http://www./
2005-04-13 14:07
无根泉
Rank: 2
等 级:新手上路
威 望:4
帖 子:853
专家分:0
注 册:2004-11-4
收藏
得分:0 

谢谢老大, 我上午做出来了,只是将就着能用,只能改一些自己建的表,在修改系统里带的表时就会出错, 代码如下: if bt<>"" then

for each c in request.Form if c<>"bt" then if x<>c and request.QueryString(x)<>request.Form(c) then sql="update "&request.QueryString("table")&" set "&c&"='"&request.Form(c)&"' where "&x&"='"&request.QueryString(x)&"'" 'response.Write sql conn.execute sql end if end if

next response.Redirect "main.asp?table="&request.querystring("table") end if

[此贴子已经被作者于2005-4-13 14:32:34编辑过]


我很菜,但我很努力!
2005-04-13 14:29
griefforyou
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:3336
专家分:0
注 册:2004-4-15
收藏
得分:0 
晕,你是一个字段执行一次更新操作。。。我的是生成SQL语句一次更新。 我的代码中有这个 if c<>x then '不修改主键(主键有可能是自动编号类型,无法直接修改) if strValue<>"" then strValue=strValue & "," strValue=strValue & c & "='" & Request.Form(c) & "'" end if

[此贴子已经被作者于2005-4-13 14:49:30编辑过]


天津网站建设 http://www./
2005-04-13 14:46
无根泉
Rank: 2
等 级:新手上路
威 望:4
帖 子:853
专家分:0
注 册:2004-11-4
收藏
得分:0 
没办法上午就那样做出来了,不过又跟你学了一招,
还有一点怎么判断最后一个字段?

我很菜,但我很努力!
2005-04-13 16:33
快速回复:动态更新数据库的问题??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048740 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved