1 HRESULT hr = NOERROR; 2 DBPROPSET dbpropset[2]; 3 DBPROP dbprop[1]; // Property array to initialize the provider. 4 DBPROP sscedbprop[2]; // Property array for SSCE security properties 5 INT i = 0; 6 IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL; 7 IUnknown *pIUnknownSession = NULL; 8 IDBInitialize *pIDBInitialize = NULL; 9 // Create an instance of the OLE DB provider. 10 hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0, 0, CLSCTX_INPROC_SERVER, 11 IID_IDBInitialize, (void**)&pIDBInitialize); 12 if(FAILED(hr)) 13 { 14 RETAILMSG(1,(TEXT("1==CoCreateInstance: %d /r/n"),GetLastError())); 15 goto CleanExit; 16 } 17 // Initialize property structures. 18 VariantInit(&dbprop[0].vValue); 19 for (i = 0; i < sizeof(sscedbprop) / sizeof(sscedbprop[0]); i++) 20 { 21 VariantInit(&sscedbprop[i].vValue); 22 } 23 // Leo:To create a new database, you must specify the DBPROP_INIT_DATASOURCE property to 24 // specify a name for the database. 25 dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE; 26 dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED; 27 dbprop[0].vValue.vt = VT_BSTR; 28 dbprop[0].vValue.bstrVal = SysAllocString(L"Encrypted.sdf"); 29 if(NULL == dbprop[0].vValue.bstrVal) 30 { 31 hr = E_OUTOFMEMORY; 32 goto CleanExit; 33 } 34 // Specify the property for encryption. 35 sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE; 36 sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED; 37 sscedbprop[0].vValue.vt = VT_BOOL; 38 sscedbprop[0].vValue.boolVal = VARIANT_TRUE; 39 // Specify the password. 40 sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD; 41 sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED; 42 sscedbprop[1].vValue.vt = VT_BSTR; 43 sscedbprop[1].vValue.bstrVal = SysAllocString(L"123456"); //密码 44 if(NULL == sscedbprop[1].vValue.bstrVal) 45 { 46 hr = E_OUTOFMEMORY; 47 goto CleanExit; 48 } 49 // Initialize the property sets. 50 dbpropset[0].guidPropertySet = DBPROPSET_DBINIT; 51 dbpropset[0].rgProperties = dbprop; 52 dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]); 53 dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT ; 54 dbpropset[1].rgProperties = sscedbprop; 55 dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]); 56 hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin,(void **)&pIDBDataSourceAdmin); 57 if(FAILED(hr)) 58 { 59 RETAILMSG(1,(TEXT("1==pIDBInitialize->QueryInterface: %d /r/n"),hr)); 60 goto CleanExit; 61 } 62 // Create and initialize the database. 63 hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset)/sizeof(dbpropset[0]), 64 dbpropset, NULL, IID_IUnknown, &pIUnknownSession); 65 if(FAILED(hr)) 66 { 67 RETAILMSG(1,(TEXT("1==pIDBDataSourceAdmin->CreateDataSource: %d /r/n"),hr)); 68 goto CleanExit; 69 } 70 // At this point, the new encrypted database is created. 71 leanExit: 72 VariantClear(&dbprop[0].vValue); 73 SysFreeString(dbprop[0].vValue.bstrVal); 74 for(i = 0;i < sizeof(sscedbprop) / sizeof(sscedbprop[0]);i++) 75 { 76 VariantClear(&sscedbprop[i].vValue); 77 } 78 // Do cleanup tasks here. 79 if(NULL != pIDBDataSourceAdmin) 80 { 81 pIDBDataSourceAdmin->Release(); 82 pIDBDataSourceAdmin = NULL; 83 } 84 if(NULL != pIDBInitialize) 85 { 86 pIDBInitialize->Release(); 87 pIDBInitialize = NULL; 88 } 89 if(NULL != pIUnknownSession) 90 { 91 pIUnknownSession->Release(); 92 pIUnknownSession = NULL; 93 } 94 return;
时间: 2024-10-16 14:51:37