`

Sybase ASE15.0.2中使用嵌入式SQL/C编程

 
阅读更多

使用C接口访问Sybase ASE,有多种方式。见到itpub论坛上有人问起,索性做一个简单的demo。发现,使用SQL/C比Open Client接口更直观,比较适合喜欢偷懒的coder.下边介绍详细过程.

1. 数据库环境

数据库iihero, 有用户spring/spring1, 使用sa用户登录,执行如下脚本(test_proc.sql),目的是创建一个示例表及数据,并为它创建一个存储过程,给后边的程序用.

最后它有一段验证过程.结果显然是3了.

2. 看我们的程序.

就是通过嵌入式SQL访问这个存储过程test_proc,并返回结果(example.cp), 内容如下:

3. 编译和链接

Sybase ASE中的SQL/C编程,需要进行预编译,编译,链接三个步骤.

首先要从命令行里进入vc6的dos环境, 就是进入控制台窗口,运行VC98/bin下边的VCVARS32.bat文件.

确保你的LIB变量有%SYBASE%/%SYBASE_OCS%/lib这个路径.

a. 预编译: (预编译命令是cpre)

E:/MyDocument/MYBOOKS/ASE/code/esqlc>cpre -CMSVC -m -O example.c example.cp
Precompilation Successful. No Errors or Warnings found.
Statistical Report:
Program name: cpre
Options specified: /m
Input file name: example.cp
Listing file name:
Target file name: example.c
ISQL file name:
Tag ID specified:
Compiler used: MSVC
Open Client version: CS_VERSION_150
Number of information messages: 11
Number of warning messages: 0
Number of error messages: 0
Number of SQL statements parsed: 11
Number of host variables declared: 6
Number of SQL cursors declared: 0
Number of dynamic SQL statements: 0
Number of stored Procedures generated: 1
Connection(s) information:
User id:
Server:
Database:

b. 编译: (它还需要%SYBASE%/%SYBASE_OCS%/include/sybesql.c编译成obj文件)

因此使用如下命令,编译两个.c文件:

E:/MyDocument/MYBOOKS/ASE/code/esqlc>cl /DDEBUG=1 /D_DEBUG=1 /DNET_DEBUG=1 /Od /
Z7 /MD /nologo /DWIN32 -Id:/sybase/OCS-15_0/include d:/sybase/OCS-15_0/include/s
ybesql.c example.c /c
sybesql.c
example.c
Generating Code...

c. 链接:

E:/MyDocument/MYBOOKS/ASE/code/esqlc>link example.obj sybesql.obj /out:example.e
xe libsybct.lib libsybcs.lib MSVCRT.lib
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

3. 最后结果:

E:/MyDocument/MYBOOKS/ASE/code/esqlc>example.exe
Begin test!
num_t123=3
End test!

上述过程只是简单的演示一下一个简单的存储过程在SYBASE ESQL/C中的使用. 有兴趣可以一试.

至于编译器,VC6, VC7/8/9,都应该支持.

之所以使用命令行,是因为不想依赖于集成开发环境.

<script type="text/javascript"><!-- google_ad_client = "ca-pub-7104628658411459"; /* wide1 */ google_ad_slot = "8564482570"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics