#include <iostream> #include <stdlib.h> #include <stdio.h> #include <Windows.h> #include <sql.h> #include <sqlext.h> #include <sqltypes.h> using namespace std; #define SNO_LEN 30 #define NAME_LEN 50 #define DEPART_LEN 100 #define SSEX_LEN 5 int main() { SQLHENV kinghenv, serverhenv; SQLHDBC kinghdbc, serverhdbc; SQLHSTMT kinghstmt, serverhstmt; SQLRETURN ret; SQLCHAR sName[NAME_LEN], sDepart[DEPART_LEN], sSex[SSEX_LEN], sSno[SNO_LEN]; SQLINTEGER sAge; SQLINTEGER cbAge = 0, cbSno = SQL_NTS, cbSex = SQL_NTS, cbName = SQL_NTS, cbDepart = SQL_NTS; ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &kinghenv); ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &serverhenv); ret = SQLSetEnvAttr(kinghenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); ret = SQLSetEnvAttr(serverhenv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0); ret = SQLAllocHandle(SQL_HANDLE_DBC, kinghenv, &kinghdbc); ret = SQLAllocHandle(SQL_HANDLE_DBC, serverhenv, &serverhdbc); SQLWCHAR *ServerName1 = (SQLWCHAR *)"KingbaseES ODBC"; SQLWCHAR *UserName1 = (SQLWCHAR *)"SYSTEM"; SQLWCHAR *Authentication1 = (SQLWCHAR *)"MANAGER"; ret = SQLConnect(kinghdbc, ServerName1, SQL_NTS, UserName1, SQL_NTS, Authentication1, SQL_NTS); if (!SQL_SUCCEEDED(ret)) { cout << "SQLConnect KingbaseES ODBC Failed" << endl; return -1; } SQLWCHAR *ServerName2 = (SQLWCHAR *)"SQLServer"; SQLWCHAR *UserName2 = (SQLWCHAR *)"sa"; SQLWCHAR *Authentication2 = (SQLWCHAR *)"sa"; ret = SQLConnect(kinghdbc, ServerName2, SQL_NTS, UserName2, SQL_NTS, Authentication2, SQL_NTS); if (!SQL_SUCCEEDED(ret)) { cout << "SQLConnect SQLServer Failed" << endl; return -1; } ret = SQLAllocHandle(SQL_HANDLE_STMT, kinghdbc, &kinghstmt); ret = SQLSetStmtAttr(kinghstmt, SQL_ATTR_ROW_BIND_TYPE, (SQLPOINTER)SQL_BIND_BY_COLUMN, SQL_IS_INTEGER); ret = SQLAllocHandle(SQL_HANDLE_STMT, serverhdbc, &serverhstmt); ret = SQLPrepare(serverhstmt, (SQLWCHAR *)"INSERT INTO STUDENT(SNO,SNAME,SSEX,SAGE,SDEPT) VALUES(?,?,?,?,?)", SQL_NTS); if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { ret = SQLBindParameter(serverhstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, SNO_LEN, 0, sSno, 0, &cbSno); ret = SQLBindParameter(serverhstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, NAME_LEN, 0, sName, 0, &cbName); ret = SQLBindParameter(serverhstmt, 3, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, 2, 0, sSex, 0, &cbSex); ret = SQLBindParameter(serverhstmt, 4, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, &sAge, 0, &cbAge); ret = SQLBindParameter(serverhstmt, 5, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, DEPART_LEN, 0, sDepart, 0, &cbDepart); } ret = SQLExecDirect(kinghstmt, (SQLWCHAR *)"SELECT * FROM STUDENT", SQL_NTS); if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) { ret = SQLBindCol(kinghstmt, 1, SQL_C_CHAR, sSno, SNO_LEN, &cbSno); ret = SQLBindCol(kinghstmt, 2, SQL_C_CHAR, sName, NAME_LEN, &cbName); ret = SQLBindCol(kinghstmt, 3, SQL_C_CHAR, sSex, SSEX_LEN, &cbSex); ret = SQLBindCol(kinghstmt, 4, SQL_C_LONG, &sAge, 0, &cbAge); ret = SQLBindCol(kinghstmt, 5, SQL_C_CHAR, sDepart, DEPART_LEN, &cbDepart); } while ((ret = SQLFetch(kinghstmt)) != SQL_NO_DATA_FOUND) { if (ret == SQL_ERROR) printf("Fetch error "); else ret = SQLExecute(serverhstmt); } SQLFreeHandle(SQL_HANDLE_STMT, kinghstmt); SQLDisconnect(kinghdbc); SQLFreeHandle(SQL_HANDLE_DBC, kinghdbc); SQLFreeHandle(SQL_HANDLE_ENV, kinghenv); SQLFreeHandle(SQL_HANDLE_STMT, serverhstmt); SQLDisconnect(serverhdbc); SQLFreeHandle(SQL_HANDLE_DBC, serverhdbc); SQLFreeHandle(SQL_HANDLE_ENV, serverhenv); return 0; }