HENV hEnv = NULL; // Env Handle from SQLAllocEnv()
HDBC hDBC = NULL; // Connection handle
RETCODE retcode;
// Allocate memory for ODBC Environment handle
SQLAllocEnv (&hEnv);
// Allocate memory for the connection handle
SQLAllocConnect (hEnv, &hDBC);
// Connect to the data source "test" using userid and password.
SQLCHAR szConnect[] = "DRIVER={SQL Server};SERVER=192.168.0.1;UID=sa;PWD=Goncely;DATABASE=testdb";
SQLCHAR szOutConn[1024];
SQLSMALLINT n(0);
retcode = SQLDriverConnect(hDBC, m_hWnd, szConnect, strlen((char*)szConnect),
szOutConn, 1024, &n, SQL_DRIVER_NOPROMPT);
if (retcode == SQL_SUCCESS || SQL_SUCCESS_WITH_INFO)
{
TRACE("connect to sql server success ");
HSTMT hStmt = NULL;// Statement handle
// Allocate memory for the statement handle
retcode = SQLAllocStmt (hDBC, &hStmt);
UCHAR szSqlStr[128]= "SELECT * from student" ;
// Prepare the SQL statement by assigning it to the statement handle
retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
// Execute the SQL statement handle
retcode = SQLExecute (hStmt);
SQLSMALLINT ColumnCount(0);
retcode = SQLNumResultCols(hStmt, &ColumnCount);
TRACE("total %d cols in db. ", ColumnCount);
char name[11];
int age;
SQLINTEGER StrLen_or_Ind;
retcode = SQLBindCol(hStmt, 1, SQL_C_CHAR, name, 24, &StrLen_or_Ind);
retcode = SQLBindCol(hStmt, 2, SQL_C_SLONG, &age, 0, &StrLen_or_Ind);
do
{
retcode = SQLFetch(hStmt);
TRACE("name: %s, age: %d. ", name, age);
}
while(retcode == SQL_SUCCESS);
// Free the allocated statement handle
SQLFreeStmt (hStmt, SQL_DROP);
// Disconnect from datasource
SQLDisconnect(hDBC);
}
else if(/*retcode == SQL_SUCCESS_WITH_INFO ||*/ retcode == SQL_ERROR)
{
TRACE("ai ");
SQLCHAR Sqlstate[6], MessageText[1024];
SQLINTEGER NativeError;
SQLSMALLINT TextLength;
SQLGetDiagRec(SQL_HANDLE_DBC, hDBC, 1, Sqlstate, &NativeError,
MessageText, 1024, &TextLength);
TRACE("%s ", MessageText);
}
else
{
TRACE("failed for others ");
}
// Free the allocated connection handle
SQLFreeConnect(hDBC);
// Free the allocated ODBC environment handle
SQLFreeEnv(hEnv);
这是vc源代码,据说能直接连接远程SQL,有谁能破解并用到狐表吗?