<%
'第一种是OLE DB方式
'"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
'第二种是ODBC方式。
'"Driver={SQL Server};Server=Aron1;Database=pubs;Uid=sa;Pwd=asdasd;"
Dim Conn '数据库连接
Dim StrDSN '数据库连接字符串
Dim Rs '命令字符串
StrDSN="driver={SQLServer};server=数据库服务器名;uid=用户名;pwd=密码;database=数据库名称
'建立和数据库master的连接
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open StrDSN
set Rs=Server.CreateObject("ADODB.RecordSet")
说明:其中/Set conn=Server.CreateObject("ADODB.Connection")为设置一个数据库的连接对象driver=()告诉连接的设备名是SQL-SERVER
server是连接的服务器的ip地址,Uid是指用户的用户名,pwd是 指的 用户的password,database是用户数据库在服务器端的数据库的名称
'********************************************************************
'********************************************************************
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
'********************************************************************
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>
1、备份
<%
SQL="backup database 数据库名 to disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"' with INIT"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server};Server=服务器名;uid=sa;pwd="
cnn.execute SQL
on error resume next
if err<>0 then
response.write "错误:"&err.Descripting
else
response.write "数据备份成功!"
end if
cnn.close
set cnn=nothing
%>
2、恢复
<%
aSQL="declare backup_cursor cursor global for select 'kill '+rtrim(spid) from master.dbo.sysprocesses where dbid=db_id('数据库名') exec sp_msforeach_worker '?'"
bSQL="Restore database 数据库名 from disk='"&Server.MapPath("backup")&"\"&"backuptext.dat"&"'"
set cnn=Server.createobject("adodb.connection")
cnn.open "driver={SQL Server};Server=服务器名;uid=sa;pwd=;database=master"
cnn.execute aSQL
cnn.execute bSQL
on error resume next
if err<>0 then
response.write "错误:"&err.Descripting
else
response.write "数据恢复成功!"
end if
cnn.close
set cnn=nothing
%>