hresulthr; dbidtablename; // name of table for new constraint dbidcolumnlist[1]; // name of column for new constraint dbidconstraintname; // name of new constraint dbpropdbprop[1]; dbpropsscedbprop[2]; // property array for ssce security p
hresult hr;
dbid tablename; // name of table for new constraint
dbid columnlist[1]; // name of column for new constraint
dbid constraintname; // name of new constraint
dbprop dbprop[1];
dbprop sscedbprop[2]; // property array for ssce security properties
dbpropset dbpropset[2];
dbconstraintdesc rgconstraintdescs[1]; // structure for constraint properties
idbinitialize *pidbinitialize = null;
idbproperties *pidbproperties = null;
idbcreatesession *pidbcreatesession = null;
itabledefinitionwithconstraints *pitbledefwithconstrt = null; // supports adding constraints
int i = 0;
// create an instance of the ole db provider
hr = cocreateinstance(clsid_sqlserverce_2_0, 0, clsctx_inproc_server,
iid_idbinitialize, (void**)&pidbinitialize);
if(failed(hr))
{
retailmsg(1,(text(2==cocreateinstance failed: 0x%x/r/n),hr));
goto cleanexit;
}
// initialize a property with name of database
// open an exsiting database mydatabase
variantinit(&dbprop[0].vvalue);
for(i = 0;i {
variantinit(&sscedbprop[i].vvalue);
}
dbprop[0].dwpropertyid = dbprop_init_datasource;
dbprop[0].dwoptions = dbpropoptions_required;
dbprop[0].vvalue.vt = vt_bstr;
dbprop[0].vvalue.bstrval = sysallocstring(lencrypted.sdf);
// 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(l123456); //密码
if(null == sscedbprop[1].vvalue.bstrval)
{
hr = e_outofmemory;
goto cleanexit;
}
// initialize the property set
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]);
//set initialization properties.
hr = pidbinitialize->queryinterface(iid_idbproperties, (void **)&pidbproperties);
if(failed(hr))
{
retailmsg(1,(text(2==pidbinitialize->queryinterface failed: 0x%x/r/n),hr));
goto cleanexit;
}
// sets properties in the data source and initialization property groups
hr = pidbproperties->setproperties(sizeof(sscedbprop) / sizeof(sscedbprop[0]), dbpropset);
if(failed(hr))
{
retailmsg(1,(text(2==pidbproperties->setproperties failed: 0x%x/r/n),hr));
goto cleanexit;
}
// initializes a data source object
hr = pidbinitialize->initialize();
if(failed(hr))
{
retailmsg(1,(text(2==pidbinitialize->initialize failed: 0x%x/r/n),hr));
goto cleanexit;
}
//只有已经创建表,以下操作才可能成功
hr = pidbinitialize->queryinterface(iid_idbcreatesession,(void**)&pidbcreatesession);
if(failed(hr))
{
retailmsg(1,(text(2==pidbinitialize->queryinterface failed: 0x%x/r/n),hr));
goto cleanexit;
}
// create a session object.
hr = pidbcreatesession->createsession(null, iid_itabledefinitionwithconstraints,
(iunknown**) &pitbledefwithconstrt);
if(failed(hr))
{
retailmsg(1,(text(2==pidbcreatesession->createsession failed: 0x%x/r/n),hr));
goto cleanexit;
}
// (this sample assumes that we have information about the testtable table database schema.)
// prepare the table name dbid as employees.
tablename.ekind = dbkind_name;
tablename.uname.pwszname = ltesttable;
// prepare the list of columns that will get the unique constraint.
// in this case, just the iid column.
columnlist[0].ekind = dbkind_name;
columnlist[0].uname.pwszname = liid;
// build the dbconstraintdesc structure needed to make the
// itabledefinitionwithconstraints::addconstraint
// call to add the constraint.
rgconstraintdescs[0].pconstraintid = &constraintname;
rgconstraintdescs[0].constrainttype = dbconstrainttype_unique;
rgconstraintdescs[0].ccolumns = 1;
rgconstraintdescs[0].rgcolumnlist = columnlist;
rgconstraintdescs[0].deferrability = 0; // sql server ce constraints are not deferrable.
// the following properties are not used in unique constraints
rgconstraintdescs[0].preferencedtableid = null;
rgconstraintdescs[0].cforeignkeycolumns = 0;
rgconstraintdescs[0].rgforeignkeycolumnlist = null;
rgconstraintdescs[0].pwszconstrainttext = null;
rgconstraintdescs[0].updaterule = dbupdelrule_noaction;
rgconstraintdescs[0].deleterule = dbupdelrule_noaction;
rgconstraintdescs[0].matchtype = dbmatchtype_none;
// add the new constraint
hr = pitbledefwithconstrt->addconstraint(&tablename, rgconstraintdescs);
if(failed(hr))
{ //0x80040e37: table does not exist.
retailmsg(1,(text(2==pitbledefwithconstrt->addconstraint: 0x%x/r/n),hr));
goto cleanexit;
}
cleanexit:
variantclear(&dbprop[0].vvalue);
sysfreestring(dbprop[0].vvalue.bstrval);
for (i = 0; i {
variantclear(&sscedbprop[i].vvalue);
}
if(null != pitbledefwithconstrt)
{
pitbledefwithconstrt->release();
pitbledefwithconstrt = null;
}
if(null != pidbcreatesession)
{
pidbcreatesession->release();
pidbcreatesession = null;
}
if(null != pidbproperties)
{
pidbproperties->release();
pidbproperties = null;
}
if(null != pidbinitialize)
{
pidbinitialize->release();
pidbinitialize = null;
}