[返回]
计算机世界2000年第10期
中国地质大学 黄孝林
目前管理信息系统已从传统的客户机/
服务器(C/S)模式转向了浏览器/ 服务器(B/S)模式,管理信息系统的核心是对数据库进行添加、修改和查询等操作,ASP
提供的ADO 数据库接口控件,使得程序员再也无需编写复杂的CGI
程序,而只要用几句简单的语句即可实现以上操作。目前有很多介绍用ASP
开发网络数据库的程序例子,但绝大部分是利用Access
作底层数据库。相对于Access 而言,SQL Server
数据库系统要复杂得多,因此在程序开发中需要多做一些工作。笔者结合自己开发管理信息系统的经验,在此试举一例,与感兴趣的朋友共同交流。
一、系统环境
PII 350,Ram 64M,WINNT Server 4.0, Service Pack4, IIS 4.0, SQL Server7.0。
二、系统功能
通过使用IE
浏览器,在网上实现对《办公文件》数据表记录的添加和查询。
三、功能实现
1 数据表的设置
* 启动SQL Server7.0 的Enterprise Manager, 在Databases
目录下增加一个新子目录“test";
* 在“test"下增加一个new table,名为“办公文件”,字段设置:文件名,char,100,
允许空;文件内容,char,4000,允许空;
* 在SQL Server 中增加一个用户,名为hxl, 密码为123,登录模式为SQL
登录,将其默认登录数据库设为“test";
* 在“test"数据库下增加一个新用户,名为hxl, 其角色设为“Dbowner"。
2 .ODBC 链接
* 在控制面板中,打开ODBC 数据源,选择系统DSN
项,单击“添加”;
* 选择“SQL Server"数据源,命名为“test";
* 选择登录模式为SQL Server 验证,登录标志为“hxl",密码为“123";
* 将默认数据库设为“test", 再测试数据库链接,接通即可。
3 . 程序文件
为了实现系统功能,在这里编辑了3 个程序文件,分别为“index.html"、“add.asp"和“query.asp",具体内容如下:(1)
index.html:
系统首页,包括两个选项,一是追加记录,二是查询,用户可单击两者之一,即可进入相应的操作过程。以下是源程序:
< html >
< head >
< meta http -equiv="Content
-Language" content="zh -cn" >
< meta http -equiv="Content
-Type" content="text/html; charset=gb2312" >
< meta name="GENERATOR" content=
"Microsoft FrontPage 4.0" >
< meta name="ProgId" content=
"FrontPage.Editor.Document" >
< title > 示例程序< /title >
< /head >
< body >
< p align="center" >< font size="5" color=
"#008000" > 欢迎使用用ASP 语言对SQL
SERVER 数据库操作示例< /font >< /P >
< p align="center" > < /P >
< p align="center" >< a href="add.asp" >
追加记录< /a >< /p >
< p align="center" >< a href="query.asp" >
&nbsp; &nbsp; &nbsp; 查询< /a >< /p >
< /body >
< /html >
(2) add.asp:
实现对《办公文件》数据表记录的添加。页面中含有一个单行文本框(Txtbiaoti)和一个滚动文本框(Txtneirong),分别用来输入文件标题和文件内容。此外,页面中还应有两个按钮,“确定(Cmdok)”和“重写(Cmdcancel)”,
单击“确定”完成记录追加,单击“重写”重新输入内容。以下是源程序:
< html >
< head >
< meta http -equiv="Content -Type"
content="text/html; charset=gb2312" >
< meta name="GENERATOR" content=
"Microsoft FrontPage 4.0" >
< meta name="ProgId" content=
"FrontPage.Editor.Document" >
< title > 追加一条新记录< /title >
< /head >
< body >
<%if request.form("cmdok")="确定" then %>
<%set dbconnection=server.createobject
("adodb.connection")
dbconnection.open"test","hxl","123"
sqlquery=insert "办公文件”
( 文件名, 文件内容) values
('request.form("Txtbiaoti")',
'request.form("Txtneirong")')
set recadd=dbconnection.execute(sqlquery)
%>
<%else %>
< p align="center" > 追加记录< /p >
< form method="POST" action="" >
< p > 文件名:< input type="text" name=
"Txtbiaoti" size="20" >< /p >
< p > 文件内容:< /p >
< p > &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; < textarea rows=
"3" name="Txtneirong" cols="60" >< /textarea ><
/p >
< p align="center" >< input type="submit" value=
"确定" name="Cmdok" > &nbsp; &nbsp; &nbsp;
< input type="reset" value="重写" name=
"Cmdcancel" >< /p >
< /form >
<%end if %>
< /body >
< /html >
(3) query.asp:
实现对《办公文件》数据表记录的查询。程序仅实现对《办公文件》中“文件名”的查询,查询结果用一个表单(Table)列出。以下是源程序:
< html >
< head >
< meta http -equiv="Content -Type" content=
"text/html; charset=gb2312" >
< meta name="GENERATOR" content=
"Microsoft FrontPage 4.0" >
< meta name="ProgId" content=
"FrontPage.Editor.Document" >
< title > 检索文件< /title >
< /head >
< body bgcolor="#ffffdd" >
<%
set dbconnection=server.createobject
("adodb.connection")
dbconnection.open "test","hxl","123"
sqlquery="SELECT 文件名FROM 办公文件"
set resultlist=dbconnection.execute(sqlquery)
%>
< center >
< font color="red" ><%=request("selectsource") %>
< /font >< font color="#008000" size="5" >< b >
有以下文件可供阅览< /b >< /font >
< hr size="5" >
< table border="1" >
< tr >
< td width="200" align="center" >
< b > 文件名< /b > < /td >
< /tr >
<%do while not resultlist.eof %>
< tr >
< td valign=center width="200" ><%=resultlist
("文件名") %>< /a >< /td >
< /tr >
<%
resultlist.movenext
loop
resultlist.close
%>
< /center >
< /TABLE >
< /body >
< /html >
四、总结
用IE 打开index.html
或将文件发布到站点,进行相应的操作,即可实现对SQL Server
数据库系统中《办公文件》数据表记录的追加和查询,至此系统目标已经达到。