在后台代码中加入:
String constr = "server=.\\sqlexpressdatabase=testuid=sapwd=123"这一种是sa用户连接的代码
//String constr = "server=.\\sqlexpressdatabase=testIntegrated security=true"这个是Windows身份验证连接的代码
SqlConnection con = new SqlConnection(constr)
con.Open()
// response.Write(con.State)
String sql = "select * from Student"
SqlCommand cmd = new SqlCommand(sql,con)
SqlDataReader dr = cmd.ExecuteReader()
while (dr.Read())
{
Response.Write(dr[1].ToString()+""+dr["name"].ToString()+"<br>")//dr[2]=dr["name"] response.write(dr.getInt32(3)+"<br>")
}
dr.Close()
con.Close()
'第一处,请注意橙色语法部分sql="select * from zhang"rs.open sql,conn,1,1
If Not (rs.bof And rs.eof) Then '不为空时显示
do while not rs.eof%>'<%= rs("hao") %>',<% rs.movenext
loop
Else
response.write "没有数据"
End If
rs.close
'第二处,请注意橙色语法部分
sql="select * from zhangdan where hao='"&request("danhao")&"'"
rs.open sql,conn,1,3
If Not(rs.bof And rs.eof) Then '不为空时,表示有数据才能更新
if request("act")="mod" then
rs.update
End If
rs.close
<%dim conn,db
dim connstr
db="db.mdb" ' access数据库文件路径
connstr="Provider=Microsoft.Jet.OLEDB.4.0Data Source="&server.MapPath(""&db&"")&"Persist Security Info=False"
set conn=server.createobject("ADODB.CONNECTION")
'连接数据库
conn.open connstr 'open datebase
'数据库结构
'文件名db.mdb
'表名admin
'字段id,自动编号
'字段username,文本
'字段userpass,文本
' 增
set Rs=server.createobject("adodb.recordset")
Sql="select * from admin"
Rs.open Sql,conn,1,3
Rs.addnew
Rs("username")="用户名"
Rs("userpass")="用户密码"
Rs.update
Rs.close
set Rs=nothing
response.Write "<script language='javascript'>alert('管理员增加成功!')'</script>"
' 删
s_id=1 '假设有个用户数据的id为1
Sql="delete * from admin where id="&s_id&""
set Rs=server.CreateObject("ADODB.recordset")
rs.open Sql,conn,1,3
response.Write("<script>alert('帐号删除成功!')</script>")
rs.close
set Rs=nothing
' 改
s_id=1 '假设有个用户数据的id为1
set Rs=server.createobject("adodb.recordset")
Sql="select * from admin where id="&s_id&""
Rs.open Sql,conn,1,3
if Rs.eof and Rs.bof then
Rs.close
set Rs=nothing
response.Write("<script language='javascript'>alert('此用户不存在!')history.back()</script>")
else
Rs("Password")="新密码"
Rs.update
Rs.close
set Rs=nothing
response.Write ("<script language='javascript'>alert('密码修改成功!')</script>")
End If
' 查单个
s_username="admin" '假设有个用户名为admin的用户数据
set Rs=server.createobject("adodb.recordset")
Sql="select * from admin where username='"&s_username&"'"
Rs.open Sql,conn,1,3
if Rs.eof and Rs.bof then
Rs.close
set Rs=nothing
response.Write("<script language='javascript'>alert('此用户不存在!')history.back()</script>")
else
s_name=Rs("username")
s_pass=Rs("userpass")
Rs.close
set Rs=nothing
' 输出查询到的信息
response.Write ("<script language='javascript'>alert('你查询的用户名"&s_name&"的密码为:"&s_pass&"')</script>")
End If
' 查列表
set Rs=server.CreateObject("Adodb.recordset")
sql="select * from admin order by id desc"
Rs.open sql,conn,1,1
if not Rs.eof then
' 循环输出查询到的信息
do while not Rs.eof
response.Write "id:"&Rs("id")&",用户名:"&Rs("username")&",密码:"&Rs("userpass")&"<br />"
Rs.movenext
loop
end if
Rs.close
set Rs=nothing
' 使用完数据库后要关闭连接资源
conn.close
set conn=nothing
%>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)