上一页 1 2 3 4 5 6 7 五.实例一瞥:
服务器安装windows 2000 Server操作系统,以Sybase 11.9.2数据库作为服务器端数据库管理数据;客户端安装Windows 98,Sybase客户端程序Open Client,以Visual FoxPro 6.0数据库作为客户端开发工具。扬长避短,相得益彰,充分发挥Sybase数据库安全性、可靠性高,管理数据量大;Visual FoxPrp 6.0数据库开发人员熟悉,开发速度快,程序体积小,运行速度快,占用资源少的特点。
以Sybase 11.9.2为例,编制一个实用的学籍管理程序,以期达到抛砖引玉的效果;数据库名称为Studentdb,只包括一个表(Student),其结构如下:
| 字段名称 |
宽度 |
注释 |
| code nchar |
(22) |
代号 |
| name char |
(8) |
姓名 |
| birthday nchar |
(8) |
生日 |
| grade int |
|
年级 |
| class int |
|
班 |
| tuition numeric |
(7,2) |
学费 |
| maths numeric |
(5,2) |
数学 |
| chinese numeric |
(5,2) |
语文 |
| physicsnumeric |
(5,2) |
物理 |
| chemistrynumeric |
(5,2) |
化学 |
| history numeric |
(5,2) |
历史 |
| geobiology numeric |
(5,2) |
地理 |
| biology numeric |
(5,2) |
生物 |
| gym numeric |
(5,2) |
体育 | 调试程序时需要注意以下问题: ①.服务器端Sybase数据库服务必须处于启动状态; ②.客户端安装必须选中“√”ODBC Driver组件; ③.客户端通过Open Client程序组中的Dsedit组件配置连接,具体连接操作过程如下:“Dsedit”→“Add Server Object”→输入服务名称(Server Name)→“OK” →“单击”选择所输入服务名称→“单击”“Server Address” →“右键”→“Modify Attributes”→“Add”→选择“TCP”协议→“Network Address”在此输入IP地址和端口号,例:10.23.12.120,5000→“Ping Server”测试是否连通,否则检查机器连接或重复上述过程。 ④.注意两种数据库数据类型的对应关系如下表:
Visual FoxPro 6.0数据库数据类型
|
Sybase数据库数据类型 |
字符型(C)
|
Char |
| nchar |
| nvarchar |
| varchar |
货币型(Y)
|
money |
| smallmoney |
数值型(N)
|
decimal |
| numeric |
浮动型(F)
|
无 |
日期型(D)
|
无 |
日期时间型(T)
|
datetime |
| Smalldatetime |
双精度型(B)
|
float |
| Real |
整型(I)
|
Int |
| smallint |
| tinyint |
逻辑型(L)
|
Bit |
备注型(M)
|
Text |
通用型(G)
|
image |
字符型(二进制)(C)
|
无 |
备注型(二进制)(M)
|
binary |
| varbinary | 关键程序段:
*连接远程数据源
PROCEDURE Activate wait window at 12,20 nowait "请稍后!正在连接远程数据源 ......" VarDriver=THISFORM.Text1.Value VarServer=THISFORM.Text2.Value VarDatabase=THISFORM.Text3.Value VarUser=THISFORM.Text4.Value VarPassword=THISFORM.Text5.Value Store sqlstringconnect("Provider=MSDASQL;DRIVER={"+alltrim(VarDriver)+"}; DSN=;SRVR="+alltrim(VarServer)+";DB="+alltrim(VarDatabase)+";UID="+alltrim(VarUser)+"; PWD="+alltrim(VarPassword)) to ConnHandle &&ConnHandle为连接句柄 if ConnHandle<0 =messagebox(space(4)+" 连接远程数据源失败!原因如下:"+chr(10); +"1.“SYBASE信息”参数配置不正确;"+chr(10); +"2.SYBASE服务("+alltrim(upper(VarServer))+")未启动。",16,"错误提示:") wait clear close all return else wait clear …… =sqldisconnect(ConnHandle) endif ENDPROC …… PROCEDURE Release =sqldisconnect(ConnHandle) close all ENDPROC | *查询
&&结果存入自定义游标MathsCursor =sqlsetprop(ConnHandle,'batchmode',.T.) &&设置一次返回所有结果集 =sqlprepare(ConnHandle,"select code,name,grade,class,maths from student where grade='"+Vargrade+"' and class='"+Varclass+"'","mathscursor") =sqlexec(ConnHandle) …… *表单中加入页框控件,表格控件 THISFORM.Pageframe1.Page1.Grid1.RecordSource="mathscursor" THISFORM.Pageframe1.Page1.Grid1.RecordSourceType=1 &&别名 THISFORM.Pageframe1.Page1.Grid1.Column1.ControlSource="mathscursor.code" THISFORM.Pageframe1.Page1.Grid1.Column2.ControlSource="mathscursor.name" THISFORM.Pageframe1.Page1.Grid1.Column3.ControlSource="mathscursor.grade" THISFORM.Pageframe1.Page1.Grid1.Column4.ControlSource="mathscursor.class" THISFORM.Pageframe1.Page1.Grid1.Column5.ControlSource="mathscursor.maths" …… | *修改
=sqlsetprop(ConnHandle,'transactions',2) &&设置人工事务处理 =sqlprepare(ConnHandle, "update student set grade=grade+1") &&新年升级处理 =sqlexec(ConnHandle) if messagebox("确定修改所输入数据信息吗?",4+32,"运行提示:")=6 =sqlcommit(ConnHandle) else =sqlrollback(ConnHandle) THISFORM.text1.setfocus endif …… | *删除
Vargrade=THISFORM.Text1.Value =sqlsetprop(ConnHandle,'transactions',2) &&设置人工事务处理 =sqlprepare(ConnHandle,"delete from student where grade='"+Vargrade+"'") =sqlexec(ConnHandle) if messagebox("确定删除所输入数据信息吗?",4+32,"运行提示:")=6 =sqlcommit(ConnHandle) else =sqlrollback(ConnHandle) THISFORM.text1.setfocus endif …… | *增加
Varcode=THISFORM.Text1.Value Varname=THISFORM.Text1.Value …… =sqlsetprop(ConnHandle,'transactions',2) &&设置人工事务处理
=sqlprepare(ConnHandle,"INSERT INTO student(code,name,birthday,grade,class,tuition,maths,chinese,physics, chemistry,history,geobiology,biology,gym); VALUES('"+Varcode+"','"+Varname+"','"+Varbirthday+"',"+Vargrade+","+Varclass+", "+Vartuition+","+Varmaths+","+Varchinese+","+Varphysics+","+Varchemistry+", "+Varhistory+","+Vargeobiology+","+Varbiology+","+Vargym+")") =sqlexec(ConnHandle) if messagebox("确定增加所输入数据信息吗?",4+32,"运行提示:")=6 =sqlcommit(ConnHandle) else =sqlrollback(ConnHandle) THISFORM.text1.setfocus endif …… | 上一页 1 2 3 4 5 6 7 |