asp连接sql server2000数据库例程
——————————————————————————–
<%
dim mode
mode=request.form("text4")
if mode=1 then
name=request.form("text1")
age=request.form("text2")
addr=request.form("text3")
if not isnumeric(age) then response.write "age must be number!<a href=javascript:history.go(-1)>try again</a>":response.end
age=clng(age)
end if
********************************************************************
与sql server2000有关的连接
可以参照它建立您的数据库连接
********************************************************************
敬请注意:
请根据情况配置strserver,struid,strsapwd,strdbname四个参数
dim strserver,struid,strsapwd,strdbname
strserver="(local)" 数据库服务器名
struid="testuser" 您的登录帐号
strsapwd="12345" 您的登录密码
strdbname="db_test_com" 您的数据库名称
dim conn 数据库连接
dim strdsn 数据库连接字符串
dim rs 命令字符串
strdsn="driver={sql server};server="&strserver&";uid="&struid&";pwd="&strsapwd&";database="&strdbname
建立和数据库master的连接
set conn = server.createobject("adodb.connection")
set rs=server.createobject("adodb.recordset")
conn.open strdsn
********************************************************************
********************************************************************
dim strsql
********************************************************************
读数据库的相关操作
sub readdb()
strsql="select * from test"
rs.open strsql,conn,1,1
if rs.eof then response.write "no record at all":exit sub
response.write "<table border=1>"
response.write "<tr>"
for i=0 to rs.fields.count-1
response.write "<td><font color=blue>"&rs.fields(i).name&"</font></td>"
next
response.write "</tr>"
while not rs.eof
response.write "<tr>"
for i=0 to rs.fields.count-1
response.write "<td>"&rs.fields(i).value&"</td>"
next
response.write "</tr>"
rs.movenext
wend
response.write "</table>"
rs.close
end sub
********************************************************************
********************************************************************
写数据库的相关操作
sub insertdata()
strsql="insert into test(name,age,addr) values("&name&","&age&","&addr&")"
rs.open strsql,conn,1,3
end sub
********************************************************************
if mode=1 then
call insertdata()
response.write "insert ok!"
elseif mode=2 then
call readdb()
end if
释放数据库连接对象
set rs=nothing
set conn=nothing
%>
<html>
<head>
<title></title>
<script language=javascript>
function clickit(flag){
var form1=document.form2
form1.text4.value=flag;
if (flag==1){
if (form1.text1.value==""){
alert("name cant empty!");
return false;
}
if (form1.text2.value==""){
alert("age cant empty!");
return false;
}
if (form1.text3.value==""){
alert("addr cant empty!");
return false;
}
}
form1.submit();
return true;
}
</script>
</head>
<body>
<form method=post name=form2>
name:<input type="text" id=text1 name=text1 size=12>
age:<input type="text" id=text2 name=text2 size=12>
city:<input type="text" id=text3 name=text3 size=12><br>
<input type="hidden" id=text4 name=text4>
<input type="button" value="write" id=button1 name=button1 onclick="clickit(1)">
<input type="button" value="read" id=button2 name=button2 onclick="clickit(2)">
</form>
</body>
</html>