Access the Sybase database with VC (CT-Library interface provided by Sybase)

zhaozj2021-02-17  50

In the front section I am doing a project, I want to use the Sybase client to connect the network Sybase database.

Before this, I have done a lot of Access, SQL Server databases, all of which are implemented in ADO technology. But this time, the leader requested the CT-Library interface provided by the Sybase database, so the database access technology known before, I didn't use the post, I was issued in 9cbs, asked this problem, but responded Very few, now this problem has been solved, I want to write, and to discuss with everyone, learn together, and give a person who will encounter this problem in the future. First, you must install the Sybase client and configure the Sybase client. Run from the program after the installation is complete

Dsedit is configured. Server is the name of the database to be accessed, as well as the IP of the Machine in the Sybase database.

PORT and access to protocol TCP; not configured, please ask the masters around you. When the configuration is completed, you can check the correctness of the configuration with ping. Let's go to program. Here, you should also let your project contain Sybase's header files and dynamic link libraries. Let the program know Sybase

function. After playing Tool ---> Options, select the Directories tab, Show Directories for select

After INCLUDE FILES, you can add Sybase's included: Example I here Sybase is installed in C: So I

It is: c: / sybase / include; Similarly, select Library Files Specify: C: / Sybase / Lib, it is OK. 1. Contain the header file. Add #include in header you want to connect to the Sybase database code.

, you can compile it. If you can't find this file, you have the specified path that you contain the specified path is incorrect. 2. Declare that two variables that connect the Sybase database must be connected. CS_CONTEXT * CONTEXT; / * Content Structure * / CS_Connection * PtrConnection; / * Connection Structure * / I personally think that using the CT-Library interface to access the Sybase database and have a lot of similarities with ADO technology,

It can be said that the access database is generally almost, all create space first, then create a connection. And initializes context: context = (cs_context *) null; 3. I define the following functions: BOOL ConnectSyBasedb (CString Struser, CString Struser, CString

Strpass; // Connect the database: True success, FALSE failed its parameters are: Database Name, Access Database

User name, password. This strDbname is the Server name when the Sybase passenger is configured. Void disconnectsybasedb (); // Disconnect Database Void ShowdBerror (INT Nerrcode); / / Display Error Information Time when connecting to the database 4. Specific connection function ***** ******* // ConnectSybaseDB (CString strDBName, CString strUser, CString strPass) {CS_RETCODE ret; char username [32], password [32]; memset (username, 0, sizeof (username)); memset (password , Sizeof (password); strcpy (username, struse); strcpy (password, strpassword); / * Assign content structure * / if ((RET = CS_CTX_ALLOC (CS_VERSION_100, & Context))! = Cs_succeed) {// No Success, call ShowDBerror to display the error code showdberror (return false;} / * Initializing Client_library * / if ((RET = CT_INIT (CS_VERSION_100))! = Cs_succeed) {showDBerror (RET); Return False;} / * Distribute connection structure * / if ((RET = CT_CON_ALLOC (CONTEXT, & PTRCONNECTION))! = Cs_succeed) {showDBerror (RET); Return False;} / * Set user name and password * / if ((RET = CT_CON_PROPS (PtrConnection, CS_SET, CS_USERNAME, Username, CS_NULLTERM, NULL))! = Cs_succeed) {ShowDBerror (RET); Return False;}

IF ((Ret = CT_CON_PROPS (PtrConnection, CS_SET, CS_Password, Password, CS_NULLTERM, NU

LL))! = Cs_succeed) {showDBerror (RET); Return False;} / * Establish connection * / char all [20]; strcpy (instance, strdbname); if ((Ret = CT_Connect (PtrConnection, (cs_char *) instance ,

SIZEOF (Instance))))! = CS_SUCCEED) {

Showdberror (RET);} else return true;} // ********** Disconnect function *********** // void disconnectsybasedb () {cs_retcode ret ; If (RET = CT_Close (CS_UNUSED))! = Cs_succeed) {showdberror (re))! = Ct_con_drop (ptronnection))! = Cs_succeed) / * Release Resource * / {ShowDBerror (RET); } IF ((RET = CT_EXIT (CS_UNUSED))! = Cs_succeed) / * Close all of the server

Connection, exit CT-L * / {ShowDBerror (RET);}}} = ((RET = CS_CTX_DROP (CONTEXT))! = Cs_succeed) / * Release Resources Occupation of Environmental Structure * / {ShowDBerror (RET);} context = (CS_CONTEXT * NULL; RETURN;} // ********** Display error code function ********** // void showdberror (int Nerrcode) {cstring strdberrorinfo; switch (Nerrcode) {CASE CS_MEM_ERROR: STRDBERRORINFO, "Surface; Case CS_pending: strcpy (strdberrorinfo," Asynchronous Network I / O "); Break; Case CS_Busy: STRCPY (strdberrorinfo," There is an asynchronous operation in the current connection! "); Break; case cs_canceled: strcpy (strdberrorinfo," operation is canceled! "); Break; case cs_end_results: struffpy (strdberrorinfo," The result processing ends from the server! ");") Break; Case CS_ROW_FAIL: STRCPY (STRDBERRORINFO, "Extract the current line of data failure!"); break; case cs_end_data: strcpy (strdberrorinfo, "data extraction!"); break; case cs_fail: strcpy (strdberrorInfo, "function failed ! "); Break; default: strcpy (strdberrorinfo," System not recognized! "); Break;} AFXMessageBox (strDberrorInfo);} The above is a simple connection to disconnect and display the error message for some of the reference. Please visit more, and

Everyone makes progress together. My mailbox: moorhen@263.net as reproduced, please Author: luckyegg

转载请注明原文地址:https://www.9cbs.com/read-30553.html

New Post(0)