Vorgehensweise: Erstellen einer Datenbank mithilfe von OLE DB (programmgesteuert)
Der OLE DB-Anbieter für SQL Server 2005 Compact Edition (SQL Server Compact Edition) legt die IDBDataSourceAdmin-Schnittstelle offen, mit der Sie Datenbanken in SQL Server Compact Edition erstellen und verwalten können. Geben Sie zum Erstellen einer neuen Datenbank die DBPROP_INIT_DATASOURCE-Eigenschaft an, um einen Namen für die Datenbank anzugeben.
Weitere Informationen finden Sie unter Erstellen von Datenbanken mit OLE DB.
Beispiel
Im folgenden Beispiel wird gezeigt, wie Sie eine verschlüsselte Datenbank mithilfe des OLE DB-Anbieters für SQL Server Compact Edition erstellen. Um eine Datenbank nur mit Kennwortschutz oder eine Standarddatenbank zu erstellen, entfernen Sie die nicht benötigten DBPROP-Strukturen.
// Object declarations
HRESULT hr = NOERROR;
DBPROPSET dbpropset[2];
DBPROP dbprop[1]; // Property array to initialize the provider.
DBPROP sscedbprop[2]; // Property array for SSCE security properties
INT i = 0;
IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL;
IUnknown *pIUnknownSession = NULL;
// Create an instance of the OLE DB provider.
hr = CoCreateInstance( CLSID_SQLSERVERCE_3_0, 0, CLSCTX_INPROC_SERVER,
IID_IDBDataSourceAdmin, (void**)& pIDBDataSourceAdmin);
if(FAILED(hr))
{
goto Exit;
}
// Initialize property structures.
VariantInit(&dbprop[0].vValue);
for (int i = 0; i < sizeof(sscedbprop) / sizeof(sscedbprop[0]); i++)
{
VariantInit(&sscedbprop[i].vValue);
}
// Specify the property with name of the database.
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(L"NewDatabase.sdf");
if(NULL == dbprop[0].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Specify the property for encryption.
sscedbprop[0].dwPropertyID = DBPROP_SSCE_ENCRYPTDATABASE;
sscedbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[0].vValue.vt = VT_BOOL;
sscedbprop[0].vValue.boolVal = VARIANT_TRUE;
// Specify the password.
sscedbprop[1].dwPropertyID = DBPROP_SSCE_DBPASSWORD;
sscedbprop[1].dwOptions = DBPROPOPTIONS_REQUIRED;
sscedbprop[1].vValue.vt = VT_BSTR;
sscedbprop[1].vValue.bstrVal = SysAllocString(L"mypassword");
if(NULL == sscedbprop[1].vValue.bstrVal)
{
hr = E_OUTOFMEMORY;
goto Exit;
}
// Initialize the property sets.
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
dbpropset[1].guidPropertySet = DBPROPSET_SSCE_DBINIT ;
dbpropset[1].rgProperties = sscedbprop;
dbpropset[1].cProperties = sizeof(sscedbprop)/sizeof(sscedbprop[0]);
// Create and initialize the database.
hr = pIDBDataSourceAdmin->CreateDataSource(sizeof(dbpropset)/sizeof(dbpropset[0]),
dbpropset, NULL, IID_IUnknown, &pIUnknownSession);
if(FAILED(hr))
{
goto Exit;
}
// At this point, the new encrypted database is created.
Exit:
// Do cleanup tasks here.
return;