简体   繁体   中英

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

My environment is: Windows 10 , Microsoft SQL Server 2017 Developer Edition.

I'm using c++ ADO to connect to sql server, but when it is connecting to the server, it throw an error, but return null error information.

Below is my codes,

// 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();
    }
}

When execute the init() and connect() , I found an error in log,

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

I have restarted my computer and sql server, but the problem is still existed.

Could someone give me any advice? Why is it so strange?

Thanks a lot in advance.

Just add ::CoInitialize(NULL); in the same function of Open , will be ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM