我要通过 VC win32 console application连接SQL SERVER数据库,但是代码怎么写?网上也有很多代码,但是都不能运行或者就是少这少那,如果哪位高手知道,请写出完整的代码告诉在下,除了win32 console application 外如果有其他的VC连接SQL Server 数据的代码 也麻烦告诉我一下,看看是不是符合要求,多少都会给您给分得:)50分一定是你的!
你好,你能加我QQ吗?5772150
分马上可以给你,还可以再多给你点,谢谢
这个问题,我不知道你的基础有多少,大体说一下,你先编一个文件,名叫ODBCTest.c
在VC++6.0中新建一个Win32 Console Application的项目,并将项目命名为ODBCTest.
然后,将已经写好的ODBCTest.c文件加入到该项目的Source Files中,编译并且执行.执行完成后,SQL Server中Student表的数据即为Kingbase中Student表的数据备份.
实验源代码.以下源码,给出了一些注释,供参考.
#include
#include
#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;
SQLINTEGER 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, &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);
/* 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(serverhstmt,"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);
}
/*执行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);
}
在VC++6.0中新建一个Win32 Console Application的项目,并将项目命名为ODBCTest.
然后,将已经写好的ODBCTest.c文件加入到该项目的Source Files中,编译并且执行.执行完成后,SQL Server中Student表的数据即为Kingbase中Student表的数据备份.
实验源代码.以下源码,给出了一些注释,供参考.
#include
#include
#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;
SQLINTEGER 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, &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);
/* 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(serverhstmt,"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);
}
/*执行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);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2005-12-03
楼上的哥们真够意思
用ODBC连接比较好用,使mfc可用,生成新类
使用mfc扩展类中的odbc类,创建的时候会让你选连接到哪个数据库,自动生成连接字符串
如果还有不明白的看msdn,网上也有msdn
用ODBC连接比较好用,使mfc可用,生成新类
使用mfc扩展类中的odbc类,创建的时候会让你选连接到哪个数据库,自动生成连接字符串
如果还有不明白的看msdn,网上也有msdn