c++连接数据库
#include <iostream> #include "windows.h" #include "sqlext.h" using namespace std; int main(){ SQLHENV henv; SQLHDBC hdbc; SQLRETURN retcode; SQLTCHAR ConnStrOut[256]; SQLTCHAR ConnStr[256] = L"DSN=DNS-MMLRESOLVE;UID=sa;PWD=850414"; SQLSMALLINT ConnStrOutLen; SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc); retcode = SQLDriverConnect(hdbc,NULL,ConnStr,SQL_NTS,ConnStrOut, sizeof(ConnStrOut),&ConnStrOutLen,SQL_DRIVER_NOPROMPT); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ cout << "ok" << endl; cout << ConnStrOutLen << endl; cout << ConnStrOut << endl; } getchar(); return 0; }
注意:
const char to SQLCHAR
SQLCHAR* query = L"SELECT tblIP.[IPAddress], tblIP.[IPStatus], tblIP.[IPType] FROM tblIP ORDER BY tblIP.[IPAddress] ASC;";