在 Access 里使用查询建立 存储过程/视图, 并使用 ASP 执行

摘要: 本文演示了 使用 Access查询 构造类似于 MSSQL 存储过程的功能, 并在 ASP 中操作插入数据,显示数据功能.

目录:
1. 在 Access 查询里建表, 名 tt

2. 在 Access 查询里新建查询, 内容为:...

3. 在 ASP 里使用 tt_insertParm 添加10条记录到 表tt
3.1 构造ASP ado 连接函数, fCreateCnn 与 fCloseCnn
3.2 组合使用 fCreateCnn, fCloseCnn, tt_insertParm 添加记录

4. 在 Access 查询里 创建 显示数据用的 存储过程/视图, 内容:...

5. 在 ASP 里使用 tt_selectParm 选取数据

6. 完.


1. 在 Access 查询里建表, 名 tt
输入以下内容到查询, 并执行, 执行后就新建表 tt 了. 
    linenum
  1. create table tt
  2. (
  3. id autoIncrement 
  4. primary key, 
  5. title varchar(255),
  6. content memo,
  7. dateandtime date
  8. )

说明: 
id 字段为自动编号, 并且为主键
title 为文本类型, 长度 255
content 为备注类型
dateandtime 为日期类型

2. 在 Access 查询里新建查询, 内容为:
    linenum
  1. parameters spTitle varchar(255),spContent varchar(255);
  2. insert into tt(title, content, dateandtime)
  3. values([spTitle], [spContent], now())

保存名为: tt_insertParm
作用为: 添加记录到 Access 的存储过程
注: spContent 字段里, 某某无法使用 memo 数据类型.

3. 在 ASP 里使用 tt_insertParm 添加10条记录到 表tt

3.1 构造ASP ado 连接函数, fCreateCnn 与 fCloseCnn
    linenum
  1. <%
  2. function fCreateCnn(cnn)
  3.     set cnn=createObject("adodb.connection")
  4. end function
  5.  
  6. function fCloseCnn(cnn)
  7.         cnn.close
  8.     set cnn=nothing
  9. end function
  10. %>

3.2 组合使用 fCreateCnn, fCloseCnn, tt_insertParm 添加记录
    linenum
  1. <% 
  2.     dim title, content
  3.         title="insert title"
  4.         content="insert content"
  5.     dim cnn, rs
  6.     dim i
  7.     call fCreateCnn(cnn)
  8.         cnn.open conn
  9.         for i=1 to 10
  10.             cnn.execute("exec tt_insertParm "&title&i&","&content&i)
  11.         next
  12.     call fCloseCnn(cnn)
  13.     if err.number=0 then response.Write "数据已添加" else response.Write "发生错误, 数据未添加"
  14. %>

4. 在 Access 查询里 创建 显示数据用的 存储过程/视图, 内容:
    linenum
  1. PARAMETERS qId Long;
  2. SELECT *
  3. FROM tt
  4. WHERE id=iif(isNull([qId]),id,[qId])

保存名为: tt_selectParm
说明: 如果 qId 参数值为空, 则选取所有数据, 否则选取 qId 对应的行

5. 在 ASP 里使用 tt_selectParm 选取数据
    linenum
  1. <% 
  2.     dim qId, fldNum, i
  3.         qId = 10
  4.     dim cnn, rs
  5.     call fCreateCnn(cnn)
  6.             cnn.open conn
  7.         set rs = cnn.execute("EXEC tt_selectParm " & qId) 
  8.         'set rs = cnn.execute("EXEC tt_selectParm null") 
  9.             fldNum=rs.fields.count-1
  10.             do until rs.eof 
  11.                  for i=0 to fldNum
  12.                     response.write rs(i)
  13.                     response.write " "
  14.                 next
  15.                    response.write "<br/>"
  16.                    rs.movenext 
  17.             loop 
  18.             rs.close
  19.         set rs=nothing
  20.     call fCloseCnn(cnn)
  21. %>

6. 完.

附注: 还有 更新,删除数据 的 存储过程/视图 没有演示, 不过相信你如果看了以上操作, 这个应该不是问题了. 
摘自:http://blog.csdn.net/btbtd/archive/2006/08/30/1145678.aspx
---------------------------------------------
生活的意义并不是与他人争高下,而在于享受努力实现目标的过程,结果是对自己行动的嘉奖。
↑面的话,越看越不痛快,应该这么说:

生活的意义就是你自己知道你要做什么,明确目标。没有目标,后面都是瞎扯!
原文地址:https://www.cnblogs.com/pengchenggang/p/1060716.html