VC连接数据库
void DBTest(HWND hWnd){
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVE={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=student;CharSet=gbk";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)10, 0);
result = SQLDriverConnect(hdbc, NULL, ConnStrIn, SQL_NTS, ConnStrOut, MAXBUFLEN,
(SQLSMALLINT *)0, SQL_DRIVER_NOPROMPT);
if(result == SQL_ERROR)
{
MessageBox(hWnd, TEXT("连接错误"), TEXT("错误"), MB_OK);
return ;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT,hdbc, &hstmt);
result = SQLPrepare(hstmt, (SQLCHAR *)"insert into t_Person(FName, FAge) values ('zhangsan', 10)", SQL_NTS);
result = SQLExecute(hstmt);
SQLFreeStmt(hstmt, SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
MessageBox(hWnd, TEXT("添加成功"), TEXT("成功"), MB_OK);
}
这是我照抄的一段连接数据库的代码(如鹏上的)
但是总是连接不上啊,求众神指导啊。。。