/// <summary> /// Do the entire process of first reading the SQL Server schema, creating a corresponding /// SQLite schema, and copying all rows from the SQL Server database to the SQLite database. /// </summary> /// <param name="sqlConnString">The SQL Server connection string</param> /// <param name="sqlitePath">The path to the generated SQLite database file</param> /// <param name="password">The password to use or NULL if no password should be used to encrypt the DB</param> /// <param name="progressReportingHandler">A handler to handle progress notifications.</param> /// <param name="selectionHandlerDefinition">The selection handler which allows the user to select which tables to convert.</param> /// <param name="selectionHandlerRecord">The selection handler which allows the user to select which tables to convert.</param> /// <param name="viewFailureHandler">The selection handler which allows the user to select which views to convert.</param> /// <param name="createTriggers">Whether or not triggers should be converted</param> /// <param name="createViews">Whether or not views should be converted</param> private static void ConvertSqlServerDatabaseToSQLiteFile(String sqlConnString, String sqlitePath, String password, SqlConversionProgressReportingHandler progressReportingHandler, SqlTableSelectionHandler selectionHandlerDefinition, SqlTableSelectionHandler selectionHandlerRecord, FailedViewDefinitionHandler viewFailureHandler, Boolean createTriggers, Boolean createViews) { // Delete the destination file (only if it exists) if (DeleteFile(sqlitePath)) { throw new Exception("File could not be deleted!"); } SqlServerSchemaReader schemaReader = new SqlServerSchemaReader(sqlConnString, Log); schemaReader.TableSchemaReaderProgressChanged += (sender, args) => { int total = args.TablesProcessed + args.TablesRemaining; int percentage = (int)((args.TablesProcessed / (Double)total) * 100); String msg = String.Format("Parsed table {0}", args.LastProcessedTable.TableName); progressReportingHandler(false, false, percentage, msg); }; schemaReader.ViewSchemaReaderProgressChanged += (sender, args) => { int total = args.ViewsProcessed + args.ViewsRemaining; int percentage = (int)((args.ViewsProcessed / (Double)total) * 100); String msg = String.Format("Parsed view {0}", args.LastProcessedView.ViewName); progressReportingHandler(false, false, percentage, msg); }; schemaReader.PopulateTableSchema(); schemaReader.PopulateViewSchema(); var includeSchema = selectionHandlerDefinition(schemaReader.Tables); schemaReader.TablesIncludeSchema = includeSchema; var includeData = selectionHandlerRecord(includeSchema); schemaReader.TablesIncludeData = includeData; // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = schemaReader.GetDatabaseSchema(); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqlitePath, ds, password, progressReportingHandler, viewFailureHandler, createViews); // Copy all rows from SQL Server tables to the newly created SQLite database var tablesToCopy = ds.Tables.Where(obj => includeData.Any(include => include.TableName == obj.TableName)).ToList(); CopySqlServerRowsToSQLiteDB(sqlConnString, sqlitePath, tablesToCopy, password, progressReportingHandler); // Add triggers based on foreign key constraints if (createTriggers) { AddTriggersForForeignKeys(sqlitePath, ds.Tables, password, progressReportingHandler); } }
/// <summary> /// Do the entire process of first reading the SQL Server schema, creating a corresponding /// SQLite schema, and copying all rows from the SQL Server database to the SQLite database. /// </summary> /// <param name="sqlConnString">The SQL Server connection string</param> /// <param name="sqlitePath">The path to the generated SQLite database file</param> /// <param name="password">The password to use or NULL if no password should be used to encrypt the DB</param> /// <param name="progressReportingHandler">A handler to handle progress notifications.</param> /// <param name="selectionHandler">The selection handler which allows the user to select which tables to convert.</param> private static void ConvertSqlServerDatabaseToSQLiteFile(String sqlConnString, String sqlitePath, String password, SqlConversionProgressReportingHandler progressReportingHandler, SqlTableSelectionHandler selectionHandlerDefinition, SqlTableSelectionHandler selectionHandlerRecord, FailedViewDefinitionHandler viewFailureHandler, Boolean createTriggers, Boolean createViews) { // Delete the destination file (only if it exists) if (DeleteFile(sqlitePath)) { throw new Exception("File could not be deleted!"); } SqlServerSchemaReader schemaReader = new SqlServerSchemaReader(sqlConnString, Log); schemaReader.TableSchemaReaderProgressChanged += (sender, args) => { int total = args.TablesProcessed + args.TablesRemaining; int percentage = (int) ((args.TablesProcessed/(Double) total)*100); String msg = String.Format("Parsed table {0}", args.LastProcessedTable.TableName); progressReportingHandler(false, false, percentage, msg); }; schemaReader.ViewSchemaReaderProgressChanged += (sender, args) => { int total = args.ViewsProcessed + args.ViewsRemaining; int percentage = (int) ((args.ViewsProcessed/(Double) total)*100); String msg = String.Format("Parsed view {0}", args.LastProcessedView.ViewName); progressReportingHandler(false, false, percentage, msg); }; schemaReader.PopulateTableSchema(); schemaReader.PopulateViewSchema(); var includeSchema = selectionHandlerDefinition(schemaReader.Tables); schemaReader.TablesIncludeSchema = includeSchema; var includeData = selectionHandlerRecord(includeSchema); schemaReader.TablesIncludeData = includeData; // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = schemaReader.GetDatabaseSchema(); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqlitePath, ds, password, progressReportingHandler, viewFailureHandler, createViews); // Copy all rows from SQL Server tables to the newly created SQLite database var tablesToCopy = ds.Tables.Where(obj => includeData.Any(include => include.TableName == obj.TableName)).ToList(); CopySqlServerRowsToSQLiteDB(sqlConnString, sqlitePath, tablesToCopy, password, progressReportingHandler); // Add triggers based on foreign key constraints if (createTriggers) { AddTriggersForForeignKeys(sqlitePath, ds.Tables, password, progressReportingHandler); } }