[英]Connect to sql server 2017 failed but return null error information when using c++ ADO
我的環境是:Windows 10,Microsoft SQL Server 2017 Developer Edition。
我正在使用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服務器,但問題仍然存在。
有人可以給我任何建議嗎? 為什么這么奇怪?
非常感謝提前。
只需添加::CoInitialize(NULL);
在Open
的相同功能中, Open
都會好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.