麻烦看看C语言利用ODBC连接Access数据库 这里SQLExecDirect怎么不成功
- C/C++ code
#include "windows.h"#include "sqlext.h"#include "string.h"#include "assert.h"#include "stdio.h"RETCODE retcode;HENV henv;HDBC hdbc;HSTMT hstmt;SWORD wColunmCouter;char* strSQL;void Connect();void Open();void End();int main(){ Connect(); Open(); End(); getchar(); return 0;}void Connect(){ retcode = ::SQLAllocEnv(&henv); assert(retcode == 0); retcode = ::SQLAllocConnect(henv,&hdbc); assert(retcode == 0); retcode = ::SQLConnect(hdbc,(SQLCHAR*)&"ODBCDemo1",SQL_NTS,NULL,0,NULL,0); assert(retcode == 0);}void Open(){ assert(hdbc); strSQL = new char[strlen("select * form Book")]; strcpy(strSQL,"select * form Book"); retcode = ::SQLAllocStmt(hdbc,&hstmt); retcode = ::SQLExecDirect(hstmt,(PUCHAR)strSQL,SQL_NTS); retcode = ::SQLNumResultCols(hstmt,&wColunmCouter); retcode = ::SQLFreeStmt(hstmt,SQL_DROP);}void End(){ ::SQLDisconnect(hdbc); ::SQLFreeConnect(hdbc); ::SQLFreeEnv(henv);}
在Open函数里面的SQLExecDirect函数,怎么弄才能执行啊?
我这里一直没看懂
[解决办法]
1.
strSQL = new char[strlen("select * form Book")];
strcpy(strSQL,"select * form Book");
有两个问题,strSQL 分配了没有释放,而且分配长度不够,没留足\0结束符的位置
2.你说执行了没有,从你的程序是看不到执行结果的,没把执行结果打印
[解决办法]
[解决办法]
LZ SQL语法错误 "select * from Book",改了这个就可以了啊!
然后就是那个字符处理要注意,虽然在你这里暂时没出现大问题,但是jackyjkchen兄说的很对!