Skip to content

Instantly share code, notes, and snippets.

@habibillah
Created December 14, 2011 06:58
Show Gist options
  • Save habibillah/1475560 to your computer and use it in GitHub Desktop.
Save habibillah/1475560 to your computer and use it in GitHub Desktop.
Copy table and data from one database(Oracle) to other database(SQlite) using XPO
protected void CopyDataToNewDatabase()
{
//get all data from current database
//(database connection strored in other place and initialize by _unitOfWork)
XPCollection<Item> items = new XPCollection<Item>(_unitOfWork);
items.Load();
//prepare new connection string to other database
string fileLocation = System.Web.Hosting.HostingEnvironment.MapPath(@"~\App_Data\multi_language.db");
string conn = DevExpress.Xpo.DB.SQLiteConnectionProvider.GetConnectionString(fileLocation);
DevExpress.Xpo.Metadata.XPDictionary dict = new DevExpress.Xpo.Metadata.ReflectionDictionary();
dict.GetDataStoreSchema(typeof(Item).Assembly);
DevExpress.Xpo.XpoDefault.Session = null;
DevExpress.Xpo.DB.IDataStore store = DevExpress.Xpo.XpoDefault.GetConnectionProvider(conn, DevExpress.Xpo.DB.AutoCreateOption.DatabaseAndSchema);
//create new session with new connection
using (UnitOfWork uow = new UnitOfWork(new DevExpress.Xpo.ThreadSafeDataLayer(dict, store), null))
{
//loap each data collection
foreach (Item item in items)
{
//save to new tables in new database
Item newItem = new Item(uow);
newItem.ItemID = item.ItemID;
newItem.SerialNumber = item.SerialNumber;
newItem.Description = item.Description;
newItem.Manufacturer = item.Manufacturer;
newItem.Supplier = item.Supplier;
newItem.ManufacturerCN = item.ManufacturerCN;
newItem.Save();
}
uow.CommitChanges();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment