⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myoledb.txt

📁 我用evc 4.0用oledb连接sql server ce数据库
💻 TXT
字号:
我用evc 4.0用oledb连接sql server ce数据库,数据库设置要用密码(123456)访问。
HRESULT          hr              = NOERROR; 
DBPROPSET        dbpropset[2]; 
DBPROP           dbprop[1]; 
DBPROP           sscedbprop[1]; 
BSTR             pwdPassword  // user input password

// Declare the provider interfaces.
IDBInitialize *  pIDBInitialize  = NULL;
IDBProperties *  pIDBProperties  = NULL;

// Initialize the data source.
hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0,
  0, 
  CLSCTX_INPROC_SERVER,
                      IID_IDBInitialize,
                  (void**) &pIDBInitialize
  );
if (FAILED(hr))
{
    //Send an error-specific message and do error handling.
    goto Exit;
}

// Initialize property structures
VariantInit(&dbprop[0].vValue);
VariantInit(&sscedbprop[0].vValue);

// Initialize Property with name of database.
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"DSDB.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
    hr = E_OUTOFMEMORY;
    goto Exit;
}

// Second property set has one property containing the provider-specific
// database password in the pwdPassword variable that was obtained from a
// dialog box (not shown).
sscedbprop[0].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_BSTR;
sscedbprop[0].vValue.bstrVal = SysAllocString(pwdPassword); 

if(NULL == sscedbprop[0].vValue.bstrVal)
{

    hr = E_OUTOFMEMORY;
    goto Exit;
}

// Initialize property set.
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);

// Initialize the provider-specific property set.
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT;
dbpropset[1].rgProperties = sscedbprop;
dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);

// Set the properties into the provider's data source object.
pIDBInitialize->QueryInterface(IID_IDBProperties,(void**)&pIDBProperties);

hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]), 
    dbpropset);
if(FAILED(hr))
{ 
AfxMessageBox(_T("NO SetProperties "));
    goto Exit;
}


// Initialize the data source.
hr = pIDBInitialize->Initialize();
if(FAILED(hr))
{
  AfxMessageBox(_T("NO Initialize"));
   goto Exit;
}

Exit:
//Clean up resources here

VariantClear(&dbprop[0].vValue); 
// Release interfaces
//
if(pIDBProperties)
{
pIDBProperties->Release(); 
}
    if (pIDBInitialize)
    {
AfxMessageBox(_T("NO Initialize"));
        pIDBInitialize->Release();
    }

return hr;
其中的sscedbprop[0].vValue.bstrVal = SysAllocString(pwdPassword); 
的pwdPassword怎么样写的,要字符转换吗?谁把这句给我补上啊,不然初始化数据源失败




Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0; data source=\my documents\DSDN_HQ.sdf
这里
Provider=Microsoft.SQLSERVER.OLEDB.CE.2.0
不需要

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -