ASP快速开发方法之数据操作

2014 年 9 月 5 日3970

  conn.asp
<%
db_path = "database/cnbruce.mdb"
Set conn= Server.CreateObject("ADODB.Connection")
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath(db_path)
conn.Open connstr
%>

  那我再改进下:
把conn.asp文件改成:
<%
Dim Conn
Dim Rs
Sub CloseDatabase
Conn.close
Set Conn = Nothing
End Sub
Sub OpenDatabase
Dim StrServer,StrUid,StrSaPwd,StrDbName
StrServer="192.168.1.1" '数据库服务器名
StrUid="sa" '您的登录帐号
StrSaPwd="" '您的登录密码
StrDbName="cnbruce.mdb" '您的数据库名称
Set Conn = Server.CreateObject("ADODB.Connection")
'用于连接ACCESS
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath(StrDbName)
'用于连接MSSQL
'Conn.ConnectionString = "Driver={sql server};driver={SQL server};server="&StrServer&";uid="&StrUid&";pwd="&StrSaPwd&";database=
"&StrDbName
set rs=server.CreateObject("ADODB.RecordSet")
conn.open
if Err Then
err.Clear
Set Conn = Nothing
GBL_CHK_TempStr = GBL_CHK_TempStr & "数据库连接错误!"
Response.Write GBL_CHK_TempStr
Response.End
End If
End Sub
%>

  showit.asp
<!--#include file="conn.asp" -->
<%
sql = "Select * from cnarticle"
opendatabase
rs.Open sql,conn,1,1
If not Rs.eof then
Do Until rs.EOF
response.write("文章标题是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入时间是:"& rs("cn_time"))
response.write("<br>文章内容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Loop
else
response.write ("暂时还没有文章")
end if
Closedatabase
%>

  showit.asp
<!--#include file="conn.asp" -->
<!--#include file="sql.asp" -->
<%
sql = "Select * from cnarticle"
set loadData=new DataTable
Thedata=loadData.SelectData(sql)
If isarray(Thedata) then
Num=ubound(Thedata,2)
for i=0 to Num
response.write("文章标题是:"& Thedata(1,i))
response.write("<br>文章作者是:"& Thedata(2,i))
response.write("<br>文章加入时间是:"& Thedata(3,i))
response.write("<br>文章内容是:"& Thedata(4,i))
response.write("<hr>")
next
else
response.write("暂时还没有文章")
End If
%>

  <%
public Function SelectDataNum(sql)
If sql<>"" then
Opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Num=ubound(Thedata,2)
Else
Closedatabase
End If
End If
SelectDataNum=Num
End Function
%>

  <%
Class DataTable
public Function UpdataSql(sql)
If sql<>"" then
Opendatabase
conn.execute(sql)
Closedatabase
End If
End Function
End Class
%>

  <%
Class DataTable
dim tempvalue
public Function DeldataSql(tableName,DelField,id)
If tableName<>"" and id<>"" then
sql="delete from "&tableName
If isnumeric(id) and instr(id,",")=0 then
sql = sql & " where "&DelField&" = "&id
Else
sql = sql & " where "&DelField&" in ("& id &")"
End If
Opendatabase
conn.execute(sql)
Closedatabase
tempvalue=true
Else
tempvalue=false
End If
DeldataSql=tempvalue
End Function
End Class
%>

  <%
'用于查询数据
Class DataTable
'查出记录
public Function SelectData(sql)
If sql<>"" then
opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Else
Closedatabase
End If
End If
SelectData=Thedata
End Function
'查出记录条数
public Function SelectDataNum(sql)
If sql<>"" then
Opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Num=ubound(Thedata,2)
Else
Closedatabase
End If
End If
SelectDataNum=Num
End Function
'使用select count(*) from tablename 查出记录有数
public Function SelectCountNum(sql)
If sql<>"" then
Opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.GetRows(-1)
Closedatabase
Num=Thedata(0,0)
Else
Closedatabase
End If
End If
SelectCountNum=Num
End Function
'将查询的数据全部生成隐藏值
public Function GetHiddenData(sql)
dim tempvalue
If sql<>"" then
Opendatabase
Rs.open sql,conn,1,1
If not Rs.eof then
Thedata=Rs.getRows(-1)
TheFieldCount=runt
For i=0 to TheFieldCount-1
TheFieldList = TheFieldList & Rs.fields(i).name & ","
Next
Closedatabase
TheField = split(TheFieldList,",")
For i=0 to TheFieldCount-1
tempvalue = tempvalue & "<input type=""hidden"""&TheField(i)&""""&TheField(i)&""" value="""&Thedata(i,0)&""" />"
Next
Else
Closedatabase
End If
End If
GetHiddenData=tempvalue
End Function
public Function UpdataSql(sql)
If sql<>"" then
Opendatabase
conn.execute(sql)
Closedatabase
End If
End Function

public Function DeldataSql(tableName,DelField,id)
dim tempvalue
If tableName<>"" and id<>"" then
sql="delete from "&tableName
If isnumeric(id) and instr(id,",")=0 then
sql = sql & " where "&DelField&" = "&id
Else
sql = sql & " where "&DelField&" in ("& id &")"
End If
Opendatabase
conn.execute(sql)
Closedatabase
tempvalue=true
Else
tempvalue=false
End If
DeldataSql=tempvalue
End Function
End Class
%>


  

标签:

网络编程 ASP

0 0