Connect to Sybase Database Using vc6.0

Source: Internet
Author: User
Tags sybase sybase client sybase database

Connect a VC to the Sybase Database
First, you must install the Sybase client and configure the Sybase client. After the installation is complete, run dsedit in the program for configuration.
Server is the name of the database to be accessed, as well as the IP address and port of the machine where the Sybase Database is located, and the access protocol TCP.
After the configuration is complete, you can use ping to check whether the configuration is correct.
Next we will go to programming.
Here, you need to include the header file and dynamic link library of Sybase in your project. Let the program understand Sybase functions.
After tools ---> options are selected, select the directories label, and select export defiles in show directories,
You can add Sybase to include: for example, Sybase is installed in C: So I specify C:/Sybase/include;
Similarly, select library files to specify C:/Sybase/lib, and then click OK.
1. Include the header file.
Add # include <ctpublic. h> to the header file that you want to connect to the Sybase Database code,
Now you can compile the file. If the header file cannot be found, it indicates that the specified path is incorrect.
2. Declare two variables required to connect to the Sybase Database.
Cs_context * context;/* content structure */
Cs_connection * ptrconnection;/* Connection Structure */
I personally think that using the CT-library interface to access the Sybase Database has many similarities with the ADO technology,
It can also be said that accessing the database is generally similar, both create a space first and then create a connection.
And initialize context: context = (cs_context *) NULL;
3. I have defined the following functions:
Bool connectsybasedb (cstring strdbname, cstring struser, cstringstrpass );
// Connect to the database: true: success; false: failure. The parameters are database name, database access username, and password.
This strdbname is the server name when the Sybase client is configured.
Void disconnectsybasedb (); // disconnect the database
Void showdberror (INT nerrcode); // displays the error message when connecting to the database.
4. Specific implementation:
/// // /// ************** Create a connection function ***********// ////////////
Connectsybasedb (cstring strdbname, cstring struser, cstring strpass)
{
Cs_retcode ret;
Char username [32], password [32];
Memset (username, 0, sizeof (username ));
Memset (password, 0, sizeof (password ));
Strcpy (username, struser );
Strcpy (password, strpassword );
/* Allocate content structure */
If (ret = cs_ctx_alloc (cs_version_100, & context ))! = Cs_succeed)
{// If the call fails, showdberror is called to display the error code.
Showdberror (RET );
Return false;
}
/* Initialize client_library */
If (ret = ct_init (context, cs_version_100 ))! = Cs_succeed)
{
Showdberror (RET );
Return false;
}
/* Allocate the connection structure */
If (ret = ct_con_alloc (context, & ptrconnection ))! = Cs_succeed)
{
Showdberror (RET );
Return false;
}
/* Set the 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, null ))! = Cs_succeed)
{
Showdberror (RET );
Return false;
}
/* Establish a connection */
Char instance [20];
Strcpy (instance, strdbname );
If (ret = ct_connect (ptrconnection, (cs_char *) instance,

Sizeof (Instance )))! = Cs_succeed)
{
Showdberror (RET );
Return false;
}
Else
Return true;
}
//// // /// ************** Disconnect the function ***********// ////////////
Void disconnectsybasedb ()
{
Cs_retcode ret;
If (ret = ct_close (ptrconnection, cs_unused ))! = Cs_succeed)
{
Showdberror (RET );
}
If (ret = ct_con_drop (ptrconnection ))! = Cs_succeed)/* release resources */
{
Showdberror (RET );
}
If (ret = ct_exit (context, cs_unused ))! = Cs_succeed)/* close all connections to the server and exit the CT-L */
{
Showdberror (RET );
}
If (ret = cs_ctx_drop (context ))! = Cs_succeed)/* release resources occupied by the Environment structure */
{
Showdberror (RET );
}
Context = (cs_context *) NULL;
Return;
}
/// // /// ************* Display the error code function ***********/ /////////////
Void showdberror (INT nerrcode)
{
Cstring strdberrorinfo;
Switch (nerrcode)
{
Case cs_mem_error:
Strcpy (strdberrorinfo, "insufficient memory or Address Allocation Error! ");
Break;
Case cs_pending:
Strcpy (strdberrorinfo, "Asynchronous Network I/O is in progress! ");
Break;
Case cs_busy:
Strcpy (strdberrorinfo, "An Asynchronous Operation is ongoing in the current connection! ");
Break;
Case cs_canceled:
Strcpy (strdberrorinfo, "Operation canceled! ");
Break;
Case cs_end_results:
Strcpy (strdberrorinfo, "processing ends from the results returned by the server! ");
Break;
Case cs_row_fail:
Strcpy (strdberrorinfo, "failed to extract data from the current row! ");
Break;
Case cs_end_data:
Strcpy (strdberrorinfo, "Data Extraction ends! ");
Break;
Case cs_fail:
Strcpy (strdberrorinfo, "function execution failed! ");
Break;
Default:
Strcpy (strdberrorinfo, "system unrecognized error! ");
Break;
}
Afxmessagebox (strdberrorinfo );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.