gpt4 book ai didi

c++ - 为什么我的代码不能与旧的访问驱动程序一起使用?

转载 作者:行者123 更新时间:2023-11-30 17:44:29 25 4
gpt4 key购买 nike

// SQLTables.cpp
// compile with: user32.lib odbc32.lib
#include <windows.h>
#include <sqlext.h>
#include <stdio.h>

// simple helper functions
int MySQLSuccess(SQLRETURN rc) {
return (rc == SQL_SUCCESS || rc == SQL_SUCCESS_WITH_INFO);
}

struct DataBinding {
SQLSMALLINT TargetType;
SQLPOINTER TargetValuePtr;
SQLINTEGER BufferLength;
SQLLEN StrLen_or_Ind;
};

void printCatalog(const struct DataBinding* catalogResult, int numCols) {
for(int i = 0; i < numCols; i++)
if (catalogResult[i].StrLen_or_Ind != SQL_NULL_DATA)
printf("Catalog Name(%d) = %s \t", i + 1, (char *)catalogResult[i].TargetValuePtr);
printf("\n");
}

// remember to disconnect and free memory, and free statements and handles
int main() {
int bufferSize = 1024, i, numCols = 18;
struct DataBinding* catalogResult = (struct DataBinding*) malloc( numCols * sizeof(struct DataBinding) );
wchar_t* dbName = (wchar_t *)malloc( sizeof(wchar_t)*bufferSize );
wchar_t* userName = (wchar_t *)malloc( sizeof(wchar_t)*bufferSize );

// declare and initialize the environment, connection, statement handles
SQLHENV henv = NULL; // Environment
SQLHDBC hdbc = NULL; // Connection handle
SQLHSTMT hstmt = NULL; // Statement handle

SQLRETURN retCode;
HWND desktopHandle = GetDesktopWindow(); // desktop's window handle
SQLWCHAR connStrbuffer[1024];
SQLSMALLINT connStrBufferLen;

retCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
retCode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, -6);
retCode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//retCode = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)10, 0);
//retCode = SQLSetConnectAttr(hdbc,fOption,(SQLPOINTER)(size_t)param,0);
//retCode = SQLDriverConnect(hdbc, desktopHandle, (SQLCHAR*)"DSN=footballDB1;UID=\"\";PWD=\"\"", SQL_NTS, (SQLCHAR*)connStrbuffer, 1024 + 1, &connStrBufferLen, SQL_DRIVER_NOPROMPT);
retCode = SQLDriverConnect(hdbc, desktopHandle, (SQLCHAR*)"DSN=footballDB;UID=\"\";PWD=\"\"", SQL_NTS, (SQLCHAR*)connStrbuffer, 1024 + 1, &connStrBufferLen, SQL_DRIVER_NOPROMPT);
retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
retCode = SQLGetInfo(hdbc, SQL_DBMS_NAME , dbName, (SQLSMALLINT)bufferSize, (SQLSMALLINT *)&bufferSize);
printf("%s\n", dbName);
retCode = SQLGetInfo(hdbc, SQL_USER_NAME, userName, (SQLSMALLINT)bufferSize, (SQLSMALLINT *)&bufferSize);

bufferSize = 1024;

// allocate memory for the binding
// free this memory when done
for ( i = 0 ; i < numCols ; i++ ) {
catalogResult[i].TargetType = SQL_C_CHAR;
catalogResult[i].BufferLength = (bufferSize + 1);
catalogResult[i].TargetValuePtr = malloc( sizeof(unsigned char)*catalogResult[i].BufferLength );
}

// setup the binding (can be used even if the statement is closed by closeStatementHandle)
for ( i = 0 ; i < numCols ; i++ )
retCode = SQLBindCol(hstmt, (SQLUSMALLINT)i + 1, catalogResult[i].TargetType, catalogResult[i].TargetValuePtr, catalogResult[i].BufferLength, &(catalogResult[i].StrLen_or_Ind));

// all catalogs query
printf( "A list of names of all catalogs\n" );
//retCode = SQLTables( hstmt, (SQLCHAR*)"%", SQL_NTS, (SQLCHAR*)NULL, SQL_NTS, (SQLCHAR*)NULL, SQL_NTS, (SQLCHAR*)NULL, SQL_NTS );
retCode = SQLTables( hstmt, (unsigned char*)NULL, SQL_NTS, (unsigned char*)NULL, SQL_NTS, (unsigned char*)NULL, SQL_NTS, (unsigned char*)"'VIEW','TABLE'", SQL_NTS );
//retCode = SQLTables( hstmt, (SQLCHAR*)NULL, 0, (SQLCHAR*)"schema1", SQL_NTS, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"'VIEW','TABLE'", SQL_NTS );
//retCode = SQLTables( hstmt, (SQLCHAR*)"", SQL_NTS, (SQLCHAR*)"%", SQL_NTS, (SQLCHAR*)"test", SQL_NTS);
//retCode = SQLColumns(hstmt, (SQLCHAR*)NULL, 0, (SQLCHAR*)NULL, SQL_NTS, (SQLCHAR*)"test", SQL_NTS, (SQLCHAR*)NULL, 0);
//retCode = SQLColumns(hstmt, (SQLCHAR*)NULL, 0, (SQLCHAR*)NULL, SQL_NTS, (SQLCHAR*)"CLAIMS_HISTORY_1", SQL_NTS, (SQLCHAR*)NULL, 0);
printf( "retCode = %d:%d\n", retCode, SQL_SUCCESS);
for ( retCode = SQLFetch(hstmt) ; MySQLSuccess(retCode) ; retCode = SQLFetch(hstmt) )
printCatalog( catalogResult, numCols );
getchar();
}

这段代码应该返回数据库中的表名,并且适用于使用 *.accdb(即 Microsoft 驱动程序)扩展名保存的访问数据库,我猜这是最新的驱动程序之一,但我不是能够弄清楚为什么它不能与 Microsoft Access (*.mdb) 驱动程序一起使用。谁能告诉我为什么会发生这种情况?

这是使用最新驱动程序创建 DSN 时的示例输出。

ACCESS
A list of names of all catalogs
retCode = 0:0
Catalog Name(1) = C:\Users\akisho02\Desktop\New folder (4)\football club db2.acc
db Catalog Name(3) = db_clubs Catalog Name(4) = TABLE Catalog
≡¡║ε½½½½½½½½■ε■ε■ε■ Catalog Name(18) =
Catalog Name(1) = C:\Users\akisho02\Desktop\New folder (4)\football club db2.acc
db Catalog Name(3) = db_items Catalog Name(4) = TABLE Catalog
≡¡║ε½½½½½½½½■ε■ε■ε■ Catalog Name(18) =
Catalog Name(1) = C:\Users\akisho02\Desktop\New folder (4)\football club db2.acc
db Catalog Name(3) = db_REGION Catalog Name(4) = TABLE Catalog

这是我使用早期驱动程序创建 DSN 时的示例输出

ACCESS
A list of names of all catalogs
retCode = 0:0
Catalog Name(1) = C:\Users\akisho02\Desktop\football club db Catalog Name(3)
≡¡║ε½½½½½½½½■ε■ε■ε■ Catalog Name(18) = Catalog Name(6) =
Catalog Name(1) = C:\Users\akisho02\Desktop\football club db Catalog Name(3)
≡¡║ε½½½½½½½½■ε■ε■ε■ Catalog Name(18) = Catalog Name(6) =
Catalog Name(1) = C:\Users\akisho02\Desktop\football club db Catalog Name(3)
≡¡║ε½½½½½½½½■ε■ε■ε■ Catalog Name(18) = Catalog Name(6) =

最佳答案

这是我在 Microsoft Access 驱动程序中看到的一个错误。我建议您在设置数据库连接时使用系统提供的最新版本。

关于c++ - 为什么我的代码不能与旧的访问驱动程序一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19980998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com