#if SQLCOMPACT
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlServerCe;
namespace Sdaleo.Systems.SQLCE
{
///
/// Specific actions for SQL CE Databases
///
public class SQLCEDatabase
{
#region Database Create / Exists
///
/// Checks to see if we can query the specified server for the Database (Existence check)
///
/// SQL CE Credentials
/// Returns true if the Database Exists, false otherwise
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseExists(IConnectDb credential, out bool bExists)
{
bExists = false;
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Check for File Existence
if (File.Exists(credential.DataSource))
{
bExists = true;
return dbError;
}
else
return DBError.Create("Database File does not exist");
}
///
/// Create a new Database with the specified Credentials
///
/// SQL CE Credentials
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseCreate(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
//if (dbError.ErrorOccured)
// return dbError;
//// First Create the Directory if it doesn't exists
//string path = Path.GetDirectoryName(credential.DataSource);
//if (!Directory.Exists(path))
// Directory.CreateDirectory(path);
//// Now try to create the database if it doesn't exists
//bool bExists = false;
//dbError = DatabaseExists(credential, out bExists);
//if (!bExists)
//{
// try
// {
// using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
// {
// engine.CreateDatabase();
// }
// }
// catch (SqlCeException e)
// {
// dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
// }
//}
return dbError;
}
#endregion
#region Database Copy / Delete
///
/// Copies a specified SQL CE Database to a new specified Database
///
/// SQL CE Credentials of Source Database
/// SQLCE Credential of Destination Database To be copied To
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseCopy(IConnectDb SourceCredential, IConnectDb DestinationCredential)
{
DBError dbError = ValidationConsts.IsCredentialValid(SourceCredential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
dbError = ValidationConsts.IsCredentialValid(DestinationCredential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Make sure the Source Database exists
bool bExists = false;
dbError = DatabaseExists(SourceCredential, out bExists);
if (!bExists)
return dbError;
// Make sure the Destination Database does NOT Exist
bExists = false;
dbError = DatabaseExists(DestinationCredential, out bExists);
if (bExists)
return DBError.Create("Destination Credential Database already exists");
try
{
File.Copy(SourceCredential.DataSource, DestinationCredential.DataSource);
}
catch (Exception ex)
{
dbError = DBError.Create(ex.Message);
}
return dbError;
}
///
/// Delete a specified SQLCE Database
///
/// SQL CE Credentials
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseDelete(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
if (dbError.ErrorOccured)
return dbError;
// Make sure the Source Database exists
bool bExists = false;
dbError = DatabaseExists(credential, out bExists);
if (!bExists)
return dbError;
try
{
File.Delete(credential.DataSource);
}
catch (Exception ex)
{
dbError = DBError.Create(ex.Message);
}
return dbError;
}
#endregion
#region Database Integrity
///
/// Shrinks the specified Database to reclaim wasted space
///
/// SQL CE Credentials
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseShrink(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
//if (dbError.ErrorOccured)
// return dbError;
//// Now try to shrink the database if it exists
//bool bExists = false;
//dbError = DatabaseExists(credential, out bExists);
//if (bExists)
//{
// try
// {
// using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
// {
// engine.Shrink();
// }
// }
// catch (SqlCeException e)
// {
// dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
// }
//}
return dbError;
}
///
/// Verifies the specified Database in case it is corrupted
///
/// SQL CE Credentials
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseVerify(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
//if (dbError.ErrorOccured)
// return dbError;
//// Now try to repair the database if it exists
//bool bExists = false;
//dbError = DatabaseExists(credential, out bExists);
//if (bExists)
//{
// try
// {
// using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
// {
// engine.Verify();
// }
// }
// catch (SqlCeException e)
// {
// dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
// }
//}
return dbError;
}
///
/// Repairs the specified Database in case it is corrupted
///
/// SQL CE Credentials
/// DBError Object with ErrorOccured, if error Occured
public static DBError DatabaseRepair(IConnectDb credential)
{
DBError dbError = ValidationConsts.IsCredentialValid(credential, DBSystem.SQL_CE);
//if (dbError.ErrorOccured)
// return dbError;
//// Now try to repair the database if it exists
//bool bExists = false;
//dbError = DatabaseExists(credential, out bExists);
//if (bExists)
//{
// try
// {
// using (SqlCeEngine engine = new SqlCeEngine(credential.ConnectionString))
// {
// // Specify null destination connection string for in-place repair
// engine.Repair(null, RepairOption.RecoverAllPossibleRows);
// }
// }
// catch (SqlCeException e)
// {
// dbError = DBMS.CreateErrorDBRetVal(DBSystem.SQL_CE, e);
// }
//}
return dbError;
}
#endregion
}
}
#endif