在做ms SqlServer 数据库编程练习的时候,为了能够看一下SqlServer的联机丛书中的例子跑起来是什么样子,就拷了里面的一个源代码来跑,没想到竟然不能通过vc6的编译!
代码如下:
- #include <stdio.h>
- #include <string.h>
- #include <windows.h>
- #include <sql.h>
- #include <sqlext.h>
- #include <odbcss.h>
- #define MAXBUFLEN 255
- SQLHENV henv = SQL_NULL_HENV;
- SQLHDBC hdbc1 = SQL_NULL_HDBC;
- SQLHSTMT hstmt1 = SQL_NULL_HSTMT;
- int main() {
- RETCODE retcode;
- // SQLBindCol variables
- SQLCHAR szName[MAXNAME+1];
- // SQLINTEGER cbName;
- // Allocate the ODBC Environment and save handle.
- retcode = SQLAllocHandle (SQL_HANDLE_ENV, NULL, &henv);
- // Notify ODBC that this is an ODBC 3.0 application.
- retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
- (SQLPOINTER) SQL_OV_ODBC3, SQL_IS_INTEGER);
- // Allocate an ODBC connection and connect.
- retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc1);
- retcode = SQLConnect(hdbc1,
- "test", SQL_NTS,"jiiming", SQL_NTS, "123", SQL_NTS);
- // Allocate a statement handle.
- retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc1, &hstmt1);
- // Execute an SQL statement directly on the statement handle.
- // Uses a default result set because no cursor attributes are set.
- unsigned char *SQLstr="select * from SC";
- retcode = SQLExecDirect(hstmt1,
- SQLstr, 17);
- // Simplified result set processing. Fetch until SQL_NO_DATA.
- // The application can be compiled with the SQLBindCol line
- // commented out to illustrate SQLGetData, or compiled with the
- // SQLGetData line commented out to illustrate SQLBindCol.
- // This sample shows that SQLBindCol is called once for the
- // result set, while SQLGetData must be called once for each
- // row in the result set.
- // retcode = SQLBindCol(hstmt1, 1, SQL_C_CHAR,
- // szName, MAXNAME, &cbName);
- while ( (retcode = SQLFetch(hstmt1) ) != SQL_NO_DATA ) {
- SQLINTEGER Sno,Cno,Grade;
- SQLINTEGER Snolen,Cnolen,Gradelen;
- // SQLGetData(hstmt1, 1, SQL_C_CHAR, szName, MAXNAME, &cbName);
- SQLGetData(hstmt1,1,SQL_C_LONG,&Sno,0, &Snolen);
- SQLGetData(hstmt1,2,SQL_C_LONG,&Cno,0, &Cnolen);
- SQLGetData(hstmt1,3,SQL_C_LONG,&Grade,0, &Gradelen);
- printf("Sno= %d/tCno = %d/tGrade = %d",Sno,Cno,Grade);
- }
- /* Clean up.*/
- SQLFreeHandle(SQL_HANDLE_STMT, hstmt1);
- SQLDisconnect(hdbc1);
- SQLFreeHandle(SQL_HANDLE_DBC, hdbc1);
- SQLFreeHandle(SQL_HANDLE_ENV, henv);
- return(0);
- }
这段代码我只改了数据库名字,用户名,密码,以及结果集的处理部分,没想到不能通过编译,错误如下:
) : error C2664: 'SQLConnect' : cannot convert parameter 2 from 'char [5]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
: error C2440: 'initializing' : cannot convert from 'char [17]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
大家帮忙看看,这是没什么呀?
本文提供了一个 MS SQL Server 数据库编程示例,使用 ODBC 连接到数据库并执行 SQL 查询。示例代码中出现了编译错误,涉及参数类型不匹配的问题。
1052

被折叠的 条评论
为什么被折叠?



