<% 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!try again":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 "" response.write "" for i=0 to rs.Fields.Count-1 response.write ""&rs.Fields(i).Name&" | " next response.write " " while not rs.EOF response.write "" for i=0 to rs.Fields.Count-1 response.write ""&rs.Fields(i).Value&" | " next response.write " " rs.MoveNext wend response.write " " 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 %>
|