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

标签 c++ c sql-server ado

我的环境是:Windows 10、Microsoft SQL Server 2017 开发版。

我使用c++ ADO连接sql server,但是当它连接到服务器时,它抛出一个错误,但返回null错误信息。

下面是我的代码,

// 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 server,但问题仍然存在。

有人可以给我任何建议吗?为什么这么奇怪?

提前非常感谢。

最佳答案

只要在Open的同一个函数中添加::CoInitialize(NULL);就可以了。

关于c++ - 使用c++ ADO连接sql server 2017失败但返回null错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56141289/

相关文章:

c++ - 在程序中使用编解码器

c++ - Boost flat_map 容器

c++ - 迭代 MappedSparseMatrix 特征中的非零元素

时钟、rdtsc 和 CLOCKS_PER_SEC

sql-server - 填充数字范围并与子表中的文本连接

c++ - 依赖于构造函数的类模板参数

c - C 应用程序的功能测试。如何使用数据驱动的 TSV 将标准输入传递给 Robot Framework 中的进程?

python - 在 C 和 NumPy 数组中的数据缓冲区之间进行转换,为 Python 接口(interface)包装 C 程序的最佳方法?

sql-server - SqlException Number是如何分配的

c# - 为数组中的每个值运行 SQL 存储过程