将KingbaseES数据库中Student表得数据备份到SQL SERVER数据库中。在应用程序运行前,已经在KingbaseES和SQL SERVER中分别
建立了STUDENT关系表:
建表代码:
CREATE TABLE Student
(
Sno CHAR(9) PRIMARY KEY, //Sno是主键
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
)
创建数据源的过程代码:
#include
#inlcude
#include
#include
#include
#include
#define SNO_LEN 30
#define NAME_LEN 50
#define DEPART_LEN 100
#define SSEX_LEN 5
int main()
{
/*Step 1 定义句柄和变量 */
//以king开头的表示的是连接KingbaseES的变量
//以server开头的表示的是连接SQLSERVER的变量
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;
SQLINTEGRR cbAge=0,cbSno=SQL_NTS,cbSex=SQL_NTS,
cbName=SQL_NTS,cbDepart=SQL_NTS;
/*Step 2 初始化环境*/
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&kinghenv);
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&setverhenv);
ret=SQLSetEnvAttr(kinghenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
/*Step 3 建立连接*/
ret=SQLAllocHandle(SQL_HANDLE_DBC,kinghenv,&kinghdbc);
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(kinghdbc,"KingbaseES ODBC",SQL_NTS,"SYSTEM",
SQL_NTS,"MANAGER",SQL_NTS);
if(!SQL_SUCCEEDED(ret))
return -1; //连接失败时返回错误值;
ret=SQLConnect(serverhdbc,"SQLServer",SQL_NTS,"sa",
SQL_NTS,"sa",SQL_NTS);
if(!SQL_SUCCEEDED(ret))
return -1; //连接失败时返回错误值;
/*Step 4 初始化语句句柄*/
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);
/*Step 5 两种方式执行语句*/
/*预编译带有参数的语句*/
ret=SQLPrepare(sercerhstmt,"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,SNAME_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);
}
/*执行SQL语句*/
ret=SQLExecDirect(kinghstmt,"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);
}
/*Step 6 处理结果集并执行预编译后的语句*/
while((ret=SQLFetch(kinghstmt))!=SQL_NO_DATA_FOUND)
{
if(ret==SQL_ERROR) printf("Fetch error/n");
else ret=SQLExecute(serverhstmt);
}
/*Step 7 中止处理*/
SQLFreeHandle(SQL_HANDLE_STMT,kinghstmt);
SQLDisconnect(kinghdbc);
SQLFreeHandle(SQL_HANDLE_DBC,kinghdbc);
SQLFreeHandle(SQL_HANDLE_ENV,kinghenv);
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHanlde(SQL_HANDLE_DBC
return 0;
}