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 ;}