MSOLEDBSQL中IDbInitialize::Initialize()始终返回E_FAIL求助
用MSOLEDBSQL驱动连接SQL Express失败:IDbInitialize::Initialize()返回E_FAIL
开发环境
- 服务器:Windows 10 上的 SQL Express
- 版本:15.0.2101.7
- 实例名:MYSURFACE\SQLEXPRESS
- OLE DB版本:19.3
问题详情
尝试通过MSOLEDBSQL驱动用C++代码连接数据库,多次尝试均失败,IDbInitialize::Initialize()始终返回E_FAIL,错误信息为"Client unable to establish connection."。已尝试不同用户ID、密码、可信连接等组合,结果均一致。
所用代码
void InitializeEnvironment() { HRESULT hr = S_OK; IDBInitialize* initializer = nullptr; IDBProperties* propertySetter = nullptr; DBPROP props[1]; LPCWSTR lpwszProviderString = L"Server=MYSURFACE\\SQLEXPRESS;" L"Database=RefDb_DEV;" L"Trusted_Connection=yes"; //L"UID=serviceusr;" //L"PWD=myoldpassword;" DBPROPSET* prgPropertySets = nullptr; ULONG cPropertySets = 0; hr = CoInitialize(nullptr); hr = CoCreateInstance(CLSID_MSOLEDBSQL19, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IDBProperties), reinterpret_cast<void**>(&propertySetter)); // set provider string props[0].dwPropertyID = DBPROP_INIT_PROVIDERSTRING; props[0].dwOptions = DBPROPOPTIONS_REQUIRED; props[0].colid = DB_NULLID; VariantInit(&(props[0].vValue)); V_VT(&(props[0].vValue)) = VT_BSTR; V_BSTR(&(props[0].vValue)) = SysAllocString(lpwszProviderString); { // set the property to the property set DBPROPSET PropertySet[1] = {}; PropertySet[0].rgProperties = &props[0]; PropertySet[0].cProperties = 1; PropertySet[0].guidPropertySet = DBPROPSET_DBINIT; // set properties and connect to server hr = propertySetter->SetProperties(sizeof(PropertySet) / sizeof(DBPROPSET), PropertySet); } hr = propertySetter->QueryInterface<IDBInitialize>(&initializer); //********This is the call that always fails********** hr = initializer->Initialize(); //***************************************** if (hr != S_OK) { IErrorInfo* errorInfo = nullptr; hr = GetErrorInfo(0, &errorInfo); if (hr == S_OK) { BSTR errorDesc, source; errorInfo->GetDescription(&errorDesc); errorInfo->GetSource(&source); } } // get properties DBPROPIDSET rgDBPropIDSet[1] = {}; DBPROPID rgDBPropID[2] = {}; rgDBPropID[0] = SSPROP_INTEGRATEDAUTHENTICATIONMETHOD; rgDBPropID[1] = SSPROP_MUTUALLYAUTHENTICATED; rgDBPropIDSet[0].rgPropertyIDs = &rgDBPropID[0]; rgDBPropIDSet[0].cPropertyIDs = 2; rgDBPropIDSet[0].guidPropertySet = DBPROPSET_SQLSERVERDATASOURCEINFO; hr = propertySetter->GetProperties(1, rgDBPropIDSet, &cPropertySets, &prgPropertySets); wprintf(L"Authentication method: %s\r\n", V_BSTR(&(prgPropertySets[0].rgProperties[0].vValue))); wprintf(L"Mutually authenticated: %s\r\n", (V_BOOL(&(prgPropertySets[0].rgProperties[1].vValue)) == VARIANT_TRUE) ? L"yes" : L"no"); }
额外尝试情况
试过错误的连接字符串关键字(比如用"User ID=..."代替"UID=...")会出现不同错误,但连接字符串正确时,总是立即返回该E_FAIL错误。使用"Server=(local)"等组合时,连接会在10-15秒后超时。
内容的提问来源于stack exchange,提问作者p-chipper
相关产品推荐
相关产品推荐

