private static KdbErrorCode SetDatabaseKey(KdbManager mgr, CompositeKey pwKey)
{
KdbErrorCode e;
bool bPassword = pwKey.ContainsType(typeof(KcpPassword));
bool bKeyFile = pwKey.ContainsType(typeof(KcpKeyFile));
string strPassword = (bPassword ? (pwKey.GetUserKey(
typeof(KcpPassword)) as KcpPassword).Password.ReadString() : string.Empty);
string strKeyFile = (bKeyFile ? (pwKey.GetUserKey(
typeof(KcpKeyFile)) as KcpKeyFile).Path : string.Empty);
if(bPassword && bKeyFile)
e = mgr.SetMasterKey(strKeyFile, true, strPassword, IntPtr.Zero, false);
else if(bPassword && !bKeyFile)
e = mgr.SetMasterKey(strPassword, false, null, IntPtr.Zero, false);
else if(!bPassword && bKeyFile)
e = mgr.SetMasterKey(strKeyFile, true, null, IntPtr.Zero, false);
else if(pwKey.ContainsType(typeof(KcpUserAccount)))
throw new Exception(KPRes.KdbWUA);
else throw new Exception(KLRes.InvalidCompositeKey);
return e;
}