C ++ connects to the database through ODBC

Source: Internet
Author: User

There are many ways to connect to the database,

Here, I am using a relatively Basic Old database connection method, that is, connecting through the ODBC sqlapi library provided by Microsoft

The specific principle is that I will not drop my book bag here. Everyone knows that I have not studied it in depth.


First, paste the code for direct query:

# Include <windows. h> # include <stdio. h> # include <SQL. h> # include <stdlib. h> # include <sqlext. h> # include <string. h> # include <iostream>/** generally, if the returned error code retcode <0, it indicates that the error code is incorrect */using namespace STD; int main () {henv; hdbc; hstmt; retcode; sqlallocenv (& henv); sqlallocconnect (henv, & hdbc); retcode = sqlconnect (hdbc, (unsigned char *) "mydb", SQL _cnt, null, 0, null, 0); // cout <retcode <Endl; If (retcode = SQL _success | retcode = SQL _success_with_info) {cout <"connection successful" <Endl ;}else {cout <"connection failed" <Endl ;}retcode = sqlallocstmt (hdbc, & hstmt ); retcode = sqlexecdirect (hstmt, (unsigned char *) "select * from student", SQL _cnt); char * SnO = new char [256]; char * sname = new char [256]; char * ssex = new char [256]; char * sage = new char [256]; char * sdept = new char [256]; long columnlen; sqlbindcol (hstmt, 1, SQL _char, SnO, 256, & columnlen); // You cannot write an error in this statement. The value 256 indicates the maximum length of the string. sqlbindcol (hstmt, 2, SQL _char, sname, 256, & columnlen); sqlbindcol (hstmt, 3, SQL _char, ssex, 256, & columnlen); sqlbindcol (hstmt, 4, SQL _char, sage, 256, & columnlen); sqlbindcol (hstmt, 5, SQL _char, sdept, 256, & columnlen); If (retcode <0) {cout <"no execution statement" <Endl;} retcode = sqlfetch (hstmt); While (retcode = SQL _row_success | retcode = SQL _row_success_with_info) {If (retcode = SQL _row_success | retcode = SQL _row_success_with_info) {printf ("% s \ t % s \ n ", sno, sname, ssex, sage, sdept); retcode = sqlfetch (hstmt) ;}} sqlfreeconnect (hdbc); sqlfreeenv (henv); Return 0 ;}

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.