MFC ODBC SQLConfigDataSource() 未处理的异常

MFC ODBC SQLConfigDataSource() Unhandled Exception

我正在尝试更新旧的 c++ MFC 程序,当我尝试创建 DSN 时,SQLConfigDataSource 导致出现未处理的异常。错误消息说:

Unhandled exception at 0x00007FFC97D89129 (KernelBase.dll): 0x0000087A (parameters: 0xFFFFFFFF887A0001, 0x0000000000000053)

代码运行正确,功能正常,但我似乎无法摆脱这个错误。

int mlen;
char* szDesc = new char[256];
sprintf_s(szDesc, 256, "DSN=%s?DBQ=%s?FIL=MicrosoftAccess?",
    IV_DATABASE_NAME, // DSN name
    sDBPath); // full file name for accdb file

mlen = strlen(szDesc);
for (int i = 0; i < mlen; i++) {
    if (szDesc[i] == '?')
        szDesc[i] = '[=10=]';
}

SQLConfigDataSource(NULL, ODBC_ADD_DSN,
    "Microsoft Access Driver (*.mdb, *.accdb)",
    (LPCSTR)szDesc);

delete szDesc;

SQLConfigDataSource 的参数格式错误。参见:

SQLConfigDataSource Function

并且:

ConfigDSN Function

Each pair is terminated with a null byte, and the entire list is terminated with a null byte. (That is, two null bytes mark the end of the list.)

我已经使用它十多年了,我这样称呼它:

if( ! bOk )//failed to open the default database for lack of DSN
{
    TRACE("Could not find DSN\n");

    BOOL ret= SQLConfigDataSource(
        NULL,
        ODBC_ADD_DSN,
        (LPSTR) "Microsoft Access Driver (*.mdb)[=10=]",
        (LPSTR) "DSN=MS Access Database[=10=]"
        "Description=MS Access Database[=10=]"
        );
    if( ! ret )
    {
        AfxMessageBox( _T("The 'Data Source Name' failed to install\nPlease call My Company\n800-555-5555") );
        return FALSE;
    }
}

请注意,前两个额外的空值是多余的,但它们没有害处。但最后一行遵循两个空,列表结尾,规则。

"Description=MS Access Database[=11=]"

您应该检查 SQLConfigDataSource 的 return 值。您可以按照诊断中的描述调用 SQLInstallerError。此外,评论指出关键字和值不应包含 ?.

安装 Microsoft Access Database Engine 2016 Redistributable 时发生错误。我安装了 2010 Redistributable,它运行完美,没有任何问题。