简体   繁体   English

连接到sql server 2017失败,但在使用c ++ ADO时返回null错误信息

[英]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. 我的环境是: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. 我正在使用c ++ ADO连接到sql server,但是当它连接到服务器时,它会抛出一个错误,但返回null错误信息。

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, 执行init()connect() ,我在日志中发现错误,

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. 我已经重新启动了我的计算机和SQL服务器,但问题仍然存在。

Could someone give me any advice? 有人可以给我任何建议吗? Why is it so strange? 为什么这么奇怪?

Thanks a lot in advance. 非常感谢提前。

Just add ::CoInitialize(NULL); 只需添加::CoInitialize(NULL); in the same function of Open , will be ok. Open的相同功能中, Open都会好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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