/// <summary> /// Restores database files, pages and objects from a .csv file data created with ExportToCSV /// </summary> /// <param name="session">the active session</param> /// <param name="csvDirectory">Path to directory containing CSV files</param> static public void ImportFromCSV(this SessionBase session, string csvDirectory) { const char fieldSeperator = ','; DirectoryInfo di = new DirectoryInfo(csvDirectory); #if WINDOWS_PHONE List <FileInfo> files = di.GetFiles("*.csv").ToList(); #else List <FileInfo> files = di.GetFiles("*.csv", SearchOption.TopDirectoryOnly).ToList(); #endif Schema schema = session.OpenSchema(false); using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Database.csv"))) { string header = textReader.ReadLine(); string dbInfoString = textReader.ReadLine(); while (dbInfoString != null) { string[] dbInfo = dbInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(dbInfo[0]); string dbName = dbInfo[1].Trim(new char[] { '"' }); Database db = null; if (dbNum < 10) { db = session.OpenDatabase(dbNum, false, false); } if (db == null) { db = session.NewDatabase(dbNum, 0, dbName); } dbInfoString = textReader.ReadLine(); } } using (StreamReader textReader = new StreamReader(Path.Combine(csvDirectory, "Page.csv"))) { string header = textReader.ReadLine(); string pageInfoString = textReader.ReadLine(); while (pageInfoString != null) { int i = 0; string[] pageInfo = pageInfoString.Split(fieldSeperator); UInt32 dbNum = UInt32.Parse(pageInfo[i++]); UInt16 pageNum = UInt16.Parse(pageInfo[i++]); UInt16 numberOfSlots = UInt16.Parse(pageInfo[i++]); UInt16 firstFreeSlot = UInt16.Parse(pageInfo[i++]); UInt64 versionNumber = UInt64.Parse(pageInfo[i++]); PageInfo.encryptionKind encryptionKind = (PageInfo.encryptionKind)Enum.Parse(typeof(PageInfo.encryptionKind), pageInfo[i++]); PageInfo.compressionKind compressed = (PageInfo.compressionKind)Enum.Parse(typeof(PageInfo.compressionKind), pageInfo[i++]); UInt32 typeVersion = UInt32.Parse(pageInfo[i]); Database db = session.OpenDatabase(dbNum, false, false); Page page = db.PageCache[pageNum]; if (page == null) { page = new Page(db, pageNum, typeVersion, numberOfSlots); page.PageInfo.Compressed = compressed; page.PageInfo.Encryption = encryptionKind; page.PageInfo.VersionNumber = versionNumber; page.PageInfo.NumberOfSlots = numberOfSlots; page.PageInfo.FirstFreeSlot = firstFreeSlot; } pageInfoString = textReader.ReadLine(); } } for (int i = 0; i < 2; i++) { foreach (FileInfo info in files) { string numberString = Regex.Match(info.Name, @"\d+").Value; if (numberString.Length > 0) { UInt32 typeShortId = UInt32.Parse(numberString); UInt16 slotNumber = (UInt16)typeShortId; if ((i == 0 && slotNumber < Schema.s_bootupTypeCount) || (i == 1 && slotNumber >= Schema.s_bootupTypeCount)) { TypeVersion tv = schema.GetTypeVersion(typeShortId, session); if (tv != null) { using (StreamReader textReader = new StreamReader(info.FullName)) { CsvReader csvReader = new CsvReader(textReader, true); string[] fileldNames = csvReader.GetFieldHeaders(); foreach (string[] record in csvReader) { tv.ObjectFromStrings(record, fileldNames, session, schema); } } } } } } } }