asp快速开发方法之数据操作实例代码

  这是我自己的心得,给大家作个参考。

  我的目的是让开发变得简单,尽可能少地考虑实现语句,更多地把精力用于思考业务逻辑。希望我的文章对大家有所启发和帮助。

  如果你对ASP不熟悉,可以先看以下教程:

  1、http://布鲁斯狼的ASP编程入门进阶

  2、www.w3schools.com的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

  Set rs = Server.CreateObject ("ADODB.Recordset")

  sql = "Select * from cnarticle"

  rs.Open sql,conn,1,1

  if rs.EOF and rs.BOF then

  response.write ("暂时还没有文章")

  else

  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

  end if

  rs.close

  Set rs = Nothing

  conn.close

  set conn=Nothing

  %>

  嗯,这是一个典型的读取数据并显示的例子,参见:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448

  嗯,确实简单。从上至下,很容易明白。但是当你对多个表进行读插删改的时候,当你的代码里有很多HTML\js混杂的时候,你会有疑问:为什么有这么多东西要重复呢?

  所以一般我们把一些简单的操作独立出来,写成类或者函数放进包含文件(include)。

  那么以上的操作我们可以使用两个文件来实现:

  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

  %>

  showit.asp

  <!--#include file="conn.asp" -->

  <%

  Set rs = Server.CreateObject ("ADODB.Recordset")

  sql = "Select * from cnarticle"

  rs.Open sql,conn,1,1

  if rs.EOF and rs.BOF then

  response.write ("暂时还没有文章")

  else

  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

  end if

  rs.close

  Set rs = Nothing

  conn.close

  set conn=Nothing

  %>

  参考:http://www.cnbruce.com/blog/showlog.asp?cat_id=26&log_id=448

  现在相对简单多了,如果有多个操作页面我们只要导入连接文件就可以了,不过还是不够简洁,哪里不简洁?

  一直在创建server,一直在写close,这样很容易出错,并且看起来与内容无关的太多。

  那我再改进下:

  把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可以这样写:

  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

  %>

  嗯,我们又少写了一些东西,这样是最简单的吗?当然不是!还可以更简单。

  使用GetRows把查询出来的数据传给一个变量,使用ubound方法取得数据记录条数。

  不明白?没关系,让我们继续往下看:

  再建个文件:sql.asp

  sql.asp

  <%

  Class selectDataTable

  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

  End Class

  %>

  嗯,复制它就可以了,现在我们的showit.asp可以简单地这样写:

  showit.asp

  <!--#include file="conn.asp" -->

  <!--#include file="sql.asp" -->

  <%

  sql = "Select * from cnarticle"

  set loadData=new selectDataTable

  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

  %>

  呵呵,这样,我们只要用两句语句就完成了数据的读取。同样的,通过在sql.asp中加入

  <%

  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

  %>

  我们就可以使用

  <%

  sql = "Select * from cnarticle"

  set loadData=new selectDataTable

  Num=loadData.SelectDataNum(sql)

  %>

  来取得记录条数,可以用于分页或者用户名是否重复的判断。

  其它的对数据记录的操作我们新建一个类,使用UpdateTable来完成操作:

  <%

  Class UpdataTable

  public Function UpdataSql(sql)

  If sql<>"" then

  Opendatabase

  conn.execute(sql)

  Closedatabase

  End If

  End Function

  End Class

  %>

  <%

  sql = "delete from cnarticle"

  set UpdateDate=new UpdataTable

  UpdateDate.UpdataSql(sql)

  %>

  当然你也这以这样写:

  <%

  sql="insert into cnarticle(cn_title,cn_author,cn_content) values(' "&whattitle&" ',' "&whoauthor&" ',' "&whatcontent&" ')"

  opendatabase

  conn.execute(sql)

  closedatabase

  %>

  考虑到可能删除语句我们会这么写:

  sql="delect from cnarticle where id in(1,3,5,6,7,8)"

  我新建一个类DeldataTable,直接使用DeldataTable.DeldataSql(tableName,DelField,id)完成记录的删除操作。

  <%

  Class DeldataTable

  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

  %>

  以下是我的sql.asp文件,请自己进行增删

  复制代码 <%

  '用于查询数据

  Class selectDataTable

  '查出记录

  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