You can access Sybase ASE using the C interface in multiple ways. When someone asked about it on the itpub Forum, simply make a simple demo. It is found that using SQL/C is more intuitive than the open client interface, and is more suitable for coder that prefers to be lazy. The detailed process is described below.
1. database environment
Database iihero, with spring/spring1. Log On with the SA user and execute the following script (test_proc. SQL) to create a sample table and data and create a stored procedure for it, for later programs.
Use iihero <br/> go </P> <p> setuser 'spring' <br/> go </P> <p> If exists (select 1 from sysobjects where name = 'test _ proc ') <br/> drop proc test_proc <br/> go </P> <p> If exists (select 1 from sysobjects where name = 't123 ') <br/> drop table t123 <br/> go </P> <p> Create Table t123 (ID int primary key, col2 varchar (32) not null) <br/> insert into t123 values (1, 'iihero') <br/> insert into t123 values (2, 'sybase ') <br/> insert into t123 values (3, 'ase') <br/> go </P> <p> Create proc test_proc (@ id_min int, @ num_t123 int output) with recompile <br/> as <br/> select @ num_t123 = count (. ID) from iihero. spring. t123 A where. id >=@ id_min <br/> go </P> <p> setuser <br/> go </P> <p> declare @ num_t123 int <br/> exec spring. test_proc 1, @ num_t123 output <br/> -- select @ num_123 = count (. ID) from iihero. spring. t123 A where. id >=1 <br/> select @ num_t123 <br/> go </P> <p>
Finally, it has a verification process. The result is obviously 3.
2. Check our program.
Access the Stored Procedure test_proc Through Embedded SQL and return the result (example. CP). The content is as follows:
# Define errexit-1 <br/> # define stdexit 0 </P> <p> # include <stdio. h> </P> <p>/* declare the sqlca. */<br/> exec SQL include sqlca; </P> <p> exec SQL begin declare section; <br/> # define typesize 13 <br/> # define tidsize 6 <br/> exec SQL end declare section; </P> <p> # define eoln '/0' </P> <p> void error_handler (void) <br/>{< br/> fprintf (stderr, "/n ** sqlcode = (% LD)", sqlca. sqlcode); </P> <p> If (sqlca. sqlerrm. sqlerr ML) <br/>{< br/> fprintf (stderr, "/n ** ASE error"); <br/> fprintf (stderr, "/n *** % s", sqlca. sqlerrm. sqlerrmc); <br/>}</P> <p> fprintf (stderr, "/n"); </P> <p> exit (errexit ); <br/>}</P> <p> void warning_handler (void) <br/>{</P> <p> If (sqlca. sqlwarn [1] = 'W') <br/>{< br/> fprintf (stderr, <br/> "/n ** data truncated. /n "); <br/>}</P> <p> If (sqlca. sqlwarn [3] = 'W') <br/>{< br/> fprintf (stderr, <br/> "/N ** insufficient host variables to store results. /n "); <br/>}< br/> return; <br/>}</P> <p> int main () <br/>{< br/> exec SQL begin declare section; <br/>/* storage for login name and password. */<br/> char username [30]; <br/> char password [30]; <br/> exec SQL end declare section; </P> <p> exec SQL begin declare section; <br/> cs_int id_min; <br/> cs_int num_t123; <br/> cs_smallint retcode; <br/> E Xec SQL end declare section; </P> <p> exec SQL whenever sqlerror call error_handler (); <br/> exec SQL whenever sqlwarning call warning_handler (); <br/> exec SQL whenever not found continue; </P> <p> strcpy (username, "Spring"); <br/> strcpy (password, "spring1 "); <br/> exec SQL connect: username identified by: password; <br/> exec SQL use iihero; </P> <p> printf ("begin test! /N "); </P> <p> id_min = 0; <br/> exec SQL Exec: retcode = test_proc: id_min,: num_t123 output; </P> <p> printf ("num_t123 = % LD/N", num_t123); </P> <p> exec SQL disconnect default; </P> <p> printf ("End test! /N "); </P> <p> return stdexit; <br/>}< br/>
3. Compilation and link
The SQL/C Programming in Sybase ASE requires three steps: Pre-compilation, compilation, and link.
First, enter the DOS environment of vc6 from the command line, that is, enter the console window and run the vcvars32.bat file under vc98/bin.
Make sure that your lib variable has the PATH % Sybase %/% sybase_ocs %/lib.
A. Pre-compile :(The pre-compiled command is 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. Compile: (it also requires % Sybase %/% sybase_ocs %/include/sybesql. C to be compiled into the OBJ file)
Therefore, use the following command to Compile two. c files:
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. Link:
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. Final result:
E:/mydocument/mybooks/ASE/code/esqlc> example.exe
Begin test!
Num_t123 = 3
End test!
The above process is just a simple demonstration of the use of a simple stored procedure in Sybase esql/C. If you are interested, try it.
As for the compiler, vc6, vc7/8/9 should all be supported.
The command line does not depend on the integrated development environment.