连接到 sql server 2017 failed but return null error information when using c++ ADO

Connect to sql server 2017 failed but return null error information when using c++ ADO

我的环境是:Windows10,微软SQLServer 2017 Developer Edition。

我正在使用 C++ ADO 连接到 sql 服务器,但是当它连接到服务器时,它抛出一个错误,但是 return 没有错误信息。

下面是我的代码,

// ms_connection.h

#import "C:\Program Files (x86)\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")

class MSConnection
{
public:

    virtual void init();
    virtual void connect();

private:

    _ConnectionPtr m_connection;

};
// ms_connection.cpp

#include "ms_connection.h"

void MSConnection::init()
{
    try
    {
        m_connection.CreateInstance("ADODB.Connection");
    }
    catch (_com_error e)
    {
        LOG_ERROR << "MSConnection failed when try to init the object, " << (const char*)e.Description();

        throw (const char*)e.Description();
    }
}

void MSConnection::connect()
{
    try
    {
        m_connection->Open(L"Provider=SQLOLEDB;Data Source=127.0.0.1, 1443;", L"root", L"haiwell", adModeUnknown);
    }
    catch (_com_error e)
    {
        LOG_ERROR << "MSConnection failed when try to connect to the server, " << (const char*)e.Description();

        throw (const char*)e.Description();
    }
}

执行init()connect()时,发现日志报错,

2019-05-15 11:00:18.343 ERROR [14408] [MSConnection::connect@36] MSConnection failed when try to connect to the server, (null)

我已经重新启动了我的电脑和sql服务器,但问题仍然存在。

有人能给我什么建议吗?为什么这么奇怪?

非常感谢。

Open的同一个函数中加上::CoInitialize(NULL);即可。