/// <summary> /// This method takes as input the connection string to an SQL Server database /// and creates a corresponding SQLite database file with a schema derived from /// the SQL Server database. /// </summary> /// <param name="sqlServerConnString">The connection string to the SQL Server database.</param> /// <param name="sqlitePath">The path to the SQLite database file that needs to get created.</param> /// <param name="password">The password to use or NULL if no password should be used to encrypt the DB</param> /// <param name="handler">A handler delegate for progress notifications.</param> /// <param name="selectionHandler">The selection handler that allows the user to select which /// tables to convert</param> /// <remarks>The method continues asynchronously in the background and the caller returned /// immediatly.</remarks> public static void ConvertSqlServerToSQLiteDatabase(string sqlServerConnString, string sqlitePath, string password, SqlConversionHandler handler, SqlTableSelectionHandler selectionHandler, FailedViewDefinitionHandler viewFailureHandler, bool createTriggers) { // Clear cancelled flag _cancelled = false; WaitCallback wc = new WaitCallback(delegate(object state) { try { _isActive = true; ConvertSqlServerDatabaseToSQLiteFile(sqlServerConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, createTriggers); _isActive = false; handler(true, true, 100, "Finished converting database"); } catch (Exception ex) { _log.Error("Failed to convert SQL Server database to SQLite database", ex); _isActive = false; handler(true, false, 100, ex.Message); } // catch }); ThreadPool.QueueUserWorkItem(wc); }
private static void AddSQLiteView(SQLiteConnection conn, ViewSchema vs, FailedViewDefinitionHandler handler) { string viewSQL = vs.ViewSQL; _log.Info("\n\n" + viewSQL + "\n\n"); SQLiteTransaction sQLiteTransaction = conn.BeginTransaction(); try { new SQLiteCommand(viewSQL, conn, sQLiteTransaction).ExecuteNonQuery(); sQLiteTransaction.Commit(); } catch (SQLiteException) { sQLiteTransaction.Rollback(); if (handler == null) { throw; } ViewSchema viewSchema = new ViewSchema(); viewSchema.ViewName = vs.ViewName; viewSchema.ViewSQL = vs.ViewSQL; string text = handler(viewSchema); if (text != null) { viewSchema.ViewSQL = text; AddSQLiteView(conn, viewSchema, handler); } } }
public override void ConvertToDatabase(string sqlServerConnectionString, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { using (SqlConnection sqlServerConnection = new SqlConnection(sqlServerConnectionString)) { ConvertToDatabase(sqlServerConnection, sqliteConnection, sqlConversionHandler, sqlTableSelectionHandler, failedViewDefinitionHandler, createTriggers); } }
private void btnDownloadData_Click(object sender, EventArgs e) { if (tbnSQLitePath.Text == "") { CommonHandler.ShowMessage(MessageType.Information, "请选择\"数据路径\""); tbnSQLitePath.Focus(); return; } string sqlConnString = GetSqlServerConnectionString("123.57.229.128", "Toyota", "sa", "mxT1@mfb"); string sqlitePath = Path.Combine(tbnSQLitePath.Text.Trim(), "readonly.db"); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { pbrProgress.Value = percent; if (done) { this.Cursor = Cursors.Default; if (success) { File.Copy(sqlitePath, Path.Combine(Path.GetDirectoryName(sqlitePath), "writeable.db"), true); CommonHandler.ShowMessage(MessageType.Information, "下载成功"); pbrProgress.Value = 0; } else { CommonHandler.ShowMessage(MessageType.Information, "下载失败\r\n" + msg); pbrProgress.Value = 0; } } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List <TableSchema> schema) { return(schema); }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { return(null); }); string password = null; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, false, false); }
private void btnStart_Click(object sender, EventArgs e) { ConversionConfiguration config = _manager.CurrentConfiguration; string sqlConnString = config.ConnectionString; this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = this.OnSqlConversionHandler; SqlTableSelectionHandler selectionHandler = this.OnSqlTableSelectionHandler; FailedViewDefinitionHandler viewFailureHandler = this.OnFailedViewDefinitionHandler; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, config.SqLiteDatabaseFilePath, config.EncryptionPassword, handler, selectionHandler, viewFailureHandler, config.CreateTriggersEnforcingForeignKeys, config.TryToCreateViews); }
private void btnDownloadDataForUpdate_Click(object sender, EventArgs e) { if (tbnSQLitePathForUpdate.Text == "") { CommonHandler.ShowMessage(MessageType.Information, "请选择\"数据路径\""); tbnSQLitePathForUpdate.Focus(); return; } string sqlConnString = GetSqlServerConnectionString("192.168.1.99", "XinHuaXin_YQ", "DSAT", "DSAT"); string sqlitePath = Path.Combine(tbnSQLitePathForUpdate.Text.Trim(), "readonly.db"); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { pbrProgressForUpdate.Value = percent; if (done) { this.Cursor = Cursors.Default; if (success) { CommonHandler.ShowMessage(MessageType.Information, "下载成功"); pbrProgressForUpdate.Value = 0; } else { CommonHandler.ShowMessage(MessageType.Information, "下载失败\r\n" + msg); pbrProgressForUpdate.Value = 0; } } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List <TableSchema> schema) { return(schema); }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { return(null); }); string password = null; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, false, false); }
private static void RunConversion(string sqlConnString) { if (string.IsNullOrEmpty(sqlConnString)) { sqlConnString = Configuration.SqlServer; } var sqlitePath = Configuration.Sqlite; var password = Configuration.Password; var generateTriggers = Configuration.ExportTriggers; var tableRegex = new Regex(Configuration.Tables, RegexOptions.IgnoreCase); SqlTableSelectionHandler selectionHandler = allTables => { var tables = new List <TableSchema>(); foreach (var table in allTables) { if (tableRegex.IsMatch(table.TableName)) { Console.WriteLine("Exporting table " + table.TableName); tables.Add(table); } } return(tables); }; SqlConversionHandler handler = (done, success, percent, msg) => { Console.WriteLine(percent + "% " + msg + (success ? "" : " - ERROR")); if (done) { Console.WriteLine("Conversion done"); } }; FailedViewDefinitionHandler viewFailureHandler = vs => { Console.WriteLine("Error on view " + vs.ViewName); return(null); }; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, generateTriggers, false); }
private void dbConvert_Load(object sender, EventArgs e) { string text = ""; string appSettings = ConfigOperation.GetAppSettings("OLD_POS_DATABASE_NAME"); text = ((!bool.Parse(ConfigOperation.GetAppSettings("OLD_POS_DATABASE_SSPI"))) ? string.Format("Data Source=(local)\\SQLExpress;Initial Catalog={0};User ID=sa;Password=1031", appSettings) : string.Format("Data Source=(local)\\SQLExpress;Initial Catalog={0};Integrated Security=SSPI;", appSettings)); string sqlitePath = Program.DataPath + "\\Old_db.db3"; Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(_003CdbConvert_Load_003Eb__1_0); SqlTableSelectionHandler selectionHandler = null; FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(_003CdbConvert_Load_003Eb__1_2); string password = "******"; bool createViews = false; bool createTriggers = false; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(text, sqlitePath, password, handler, selectionHandler, viewFailureHandler, createTriggers, createViews); }
private static void AddSQLiteView(SQLiteConnection conn, ViewSchema vs, FailedViewDefinitionHandler handler) { // Prepare a CREATE VIEW DDL statement String stmt = vs.ViewSQL; // Execute the query in order to actually create the view. SQLiteTransaction tx = conn.BeginTransaction(); try { var cmd = new SQLiteCommand(stmt, conn, tx); cmd.ExecuteNonQuery(); tx.Commit(); } catch (SQLiteException ex) { Log.Error("Error in \"AddSQLiteView\"", ex); tx.Rollback(); // Rethrow the exception if it the caller didn't supply a handler. if (handler == null) { throw; } var updated = new ViewSchema(); updated.ViewName = vs.ViewName; updated.ViewSQL = vs.ViewSQL; // Ask the user to supply the new view definition SQL statement String sql = handler(updated); if (sql != null) { // Try to re-create the view with the user-supplied view definition SQL updated.ViewSQL = sql; AddSQLiteView(conn, updated, handler); } } }
private void btnStart_Click(object sender, EventArgs e) { if (!EnsureSaveLocationExists()) { MessageBox.Show("Specified save location is in a directory that does not exist!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ConversionConfiguration config = _manager.CurrentConfiguration; string sqlConnString = config.ConnectionString; Cursor = Cursors.WaitCursor; SqlConversionProgressReportingHandler progressReportingHandler = OnSqlConversionProgressReportingHandler; SqlTableSelectionHandler selectionHandlerDefinition = OnSqlTableDefinitionSelectionHandler; SqlTableSelectionHandler selectionHandlerRecords = OnSqlTableRecordSelectionHandler; FailedViewDefinitionHandler viewFailureHandler = OnFailedViewDefinitionHandler; var filePathWithReplacedEnvironmentValues = Environment.ExpandEnvironmentVariables(config.SqLiteDatabaseFilePath); SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, filePathWithReplacedEnvironmentValues, config.EncryptionPassword, progressReportingHandler, selectionHandlerDefinition, selectionHandlerRecords, viewFailureHandler, config.CreateTriggersEnforcingForeignKeys, config.TryToCreateViews); }
public override void ConvertToDatabase(SqlConnection sqlServerConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelecttionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { // Clear cancelled flag _cancelled = false; //WaitCallback wc = new WaitCallback(delegate(object state) //{ try { _isActive = true; ConvertSourceDatabaseToDestination(sqlServerConnection, sqliteConnection, sqlConversionHandler, sqlTableSelecttionHandler, failedViewDefinitionHandler, createTriggers); _isActive = false; sqlConversionHandler(true, true, 100, "Finished converting database"); } catch (Exception ex) { //Logging.Log(LogLevel.Error, string.Format("Failed to convert SQL Server database to SQLite database: {0}", FileLogger.GetInnerException(ex).Message)); //Logging.HandleException(ex); _isActive = false; sqlConversionHandler(true, false, 100, ex.Message); } //}); //ThreadPool.QueueUserWorkItem(wc); }
/// <summary> /// This method takes as input the connection string to an SQL Server database /// and creates a corresponding SQLite database file with a schema as retrieved from /// the SQL Server database. /// </summary> /// <param name="sqlServerConnString">The connection string to the SQL Server database.</param> /// <param name="sqlitePath">The path to the SQLite database file that needs to get created.</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 delegate for progress notifications.</param> /// <param name="selectionHandlerDefinition">The selection handler that allows the user to select which tables to include in the converted SQLite database.</param> /// /// <param name="selectionHandlerRecord">The selection handler that allows the user to select which tables to include the data of in the converted SQLite database.</param> /// <remarks>The method continues asynchronously in the background and the caller returns immediately.</remarks> public static Task ConvertSqlServerToSQLiteDatabase(string sqlServerConnString, string sqlitePath, string password, SqlConversionProgressReportingHandler progressReportingHandler, SqlTableSelectionHandler selectionHandlerDefinition, SqlTableSelectionHandler selectionHandlerRecord, FailedViewDefinitionHandler viewFailureHandler, Boolean createTriggers, Boolean createViews) { // Clear cancelled flag _cancelled = false; var task = Task.Factory.StartNew(() => { try { _isActive = true; String sqlitePathResolved = TemplateToFilename(sqlitePath); ConvertSqlServerDatabaseToSQLiteFile(sqlServerConnString, sqlitePathResolved, password, progressReportingHandler, selectionHandlerDefinition, selectionHandlerRecord, viewFailureHandler, createTriggers, createViews); _isActive = false; progressReportingHandler(true, true, 100, "Finished converting database"); } catch (Exception ex) { _log.Error("Failed to convert SQL Server database to SQLite database", ex); _isActive = false; progressReportingHandler(true, false, 100, ex.ToString()); } }); return task; }
/// <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="handler">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, SqlConversionHandler handler, SqlTableSelectionHandler selectionHandler, FailedViewDefinitionHandler viewFailureHandler, bool createTriggers) { // Delete the target file if it exists already. if (File.Exists(sqlitePath)) File.Delete(sqlitePath); // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = ReadSqlServerSchema(sqlConnString, handler, selectionHandler); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqlitePath, ds, password, handler, viewFailureHandler); // Copy all rows from SQL Server tables to the newly created SQLite database CopySqlServerRowsToSQLiteDB(sqlConnString, sqlitePath, ds.Tables, password, handler); // Add triggers based on foreign key constraints if (createTriggers) AddTriggersForForeignKeys(sqlitePath, ds.Tables, password, handler); }
public override void ConvertToDatabase(ConversionHandler conversionHandler, TableSelectionHandler tableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { base.ConvertToDatabase(conversionHandler, tableSelectionHandler, failedViewDefinitionHandler, createTriggers); }
private void btnSQLiteSqlServer_Click(object sender, EventArgs e) { string tempFilePath = string.Empty; string SqlServerPath = string.Empty; string sqlConnString; string dbname; string tempDirPath = Path.GetTempPath() + @"\SqlConverter"; if (Directory.Exists(tempDirPath)) { Directory.Delete(tempDirPath, true); } System.IO.Directory.CreateDirectory(tempDirPath); DirectoryInfo tempDirInfo = new DirectoryInfo(tempDirPath); DirectorySecurity tempDirSecurity = tempDirInfo.GetAccessControl(); tempDirSecurity.AddAccessRule(new FileSystemAccessRule("everyone", FileSystemRights.FullControl, AccessControlType.Allow)); tempDirInfo.SetAccessControl(tempDirSecurity); string SQLitePath = Path.GetFullPath(txtSQLitePath.Text); if (!File.Exists(SQLitePath)) { MessageBox.Show("Input file " + SQLitePath + " not found.", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (txtSqlServerPath.Text != string.Empty) { tempFilePath = Path.GetFullPath(tempDirPath + @"\" + Path.GetFileName(txtSqlServerPath.Text)); SqlServerPath = Path.GetFullPath(txtSqlServerPath.Text); if (cboWhatToCopy.SelectedIndex == 2) // ie if we are copying into an existing database { if (!File.Exists(SqlServerPath)) { MessageBox.Show("Output file '" + SqlServerPath + "' not found.", "File not found", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } System.IO.File.Copy(SqlServerPath, tempFilePath); } else { if (File.Exists(SqlServerPath)) { DialogResult result = MessageBox.Show("Replace existing file '" + SqlServerPath + "'?", "Confirm replace file", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (result != DialogResult.OK) { return; } } } string constr; if (cbxIntegrated.Checked) { constr = GetSqlServerConnectionString(txtSqlAddress.Text, "master"); } else { constr = GetSqlServerConnectionString(txtSqlAddress.Text, "master", txtUserDB.Text, txtPassDB.Text); } using (SqlConnection conn = new SqlConnection(constr)) { conn.Open(); string queryString = "CREATE DATABASE SqlConverter on (NAME=N'" + Path.GetFileNameWithoutExtension(txtSqlServerPath.Text) + "',FILENAME=N'" + tempFilePath + "')"; if (cboWhatToCopy.SelectedIndex == 2) // ie if we are copying into an existing database { queryString += " FOR ATTACH"; } SqlCommand query = new SqlCommand(queryString, conn); query.ExecuteNonQuery(); dbname = "SqlConverter"; } } else { dbname = (string)cboDatabases.SelectedItem; } if (cbxIntegrated.Checked) { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, dbname); } else { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, dbname, txtUserDB.Text, txtPassDB.Text); } this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { UpdateSensitivity(); lblMessage.Text = msg; pbrProgress.Value = percent; if (done) { if (txtSqlServerPath.Text != string.Empty) { dropSqlConverterDatabase(); if (success) { System.IO.File.Copy(tempFilePath, SqlServerPath, true); } Directory.Delete(tempDirPath, true); } if (success) { MessageBox.Show(this, msg, "Conversion Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { if (!_shouldExit) { MessageBox.Show(this, msg, "Conversion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { Application.Exit(); } } btnSQLiteSqlServer.Enabled = true; this.Cursor = Cursors.Default; UpdateSensitivity(); } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List <TableSchema> schema) { List <TableSchema> updated = null; Invoke(new MethodInvoker(delegate { // Allow the user to select which tables to include by showing him the // table selection dialog. TableSelectionDialog dlg = new TableSelectionDialog(); DialogResult res = dlg.ShowTables(schema, this); if (res == DialogResult.OK) { updated = dlg.IncludedTables; } })); return(updated); }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { string updated = null; Invoke(new MethodInvoker(delegate { ViewFailureDialog dlg = new ViewFailureDialog(); dlg.View = vs; DialogResult res = dlg.ShowDialog(this); if (res == DialogResult.OK) { updated = dlg.ViewSQL; } else { updated = null; } })); return(updated); }); string password = txtPassword.Text.Trim(); if (!cbxEncrypt.Checked) { password = null; } bool copyStructure = (cboWhatToCopy.SelectedIndex != 2); bool copyData = (cboWhatToCopy.SelectedIndex != 1); SQLiteToSqlServer.ConvertSQLiteToSqlServerDatabase(sqlConnString, SQLitePath, password, handler, selectionHandler, viewFailureHandler, copyStructure, copyData); }
/// <summary> /// Creates the SQLite database from the schema read from the SQL Server. /// </summary> /// <param name="sqlitePath">The path to the generated DB file.</param> /// <param name="schema">The schema of the SQL server database.</param> /// <param name="password">The password to use for encrypting the DB or null if non is needed.</param> /// <param name="progressReportingHandler">A handle for progress notifications.</param> private static void CreateSQLiteDatabase(string sqlitePath, DatabaseSchema schema, string password, SqlConversionProgressReportingHandler progressReportingHandler, FailedViewDefinitionHandler viewFailureHandler, bool createViews) { _log.Debug("Creating SQLite database..."); // Create the SQLite database file SQLiteConnection.CreateFile(sqlitePath); _log.Debug("SQLite file was created successfully at [" + sqlitePath + "]"); // Connect to the newly created database string sqliteConnString = CreateSQLiteConnectionString(sqlitePath, password); // Create all tables in the new database Object stateLocker = new Object(); int tableCount = 0; var orderedTables = schema.Tables.OrderBy(obj => obj.TableName).ToList(); foreach (var dt in orderedTables) { using (var conn = new SQLiteConnection(sqliteConnString)) { conn.Open(); try { AddSQLiteTable(conn, dt); } catch (Exception ex) { _log.Error("AddSQLiteTable failed", ex); throw; } lock (stateLocker) { tableCount++; } CheckCancelled(); progressReportingHandler(false, true, (int)(tableCount * 50.0 / schema.Tables.Count), String.Format("Added table [{0}] to SQLite", dt.TableName)); _log.Debug("added schema for SQLite table [" + dt.TableName + "]"); } } // Create all views in the new database int viewCount = 0; if (createViews) { var orderedViews = schema.Views.OrderBy(obj => obj.ViewName).ToList(); foreach (var vs in orderedViews) { using (var conn = new SQLiteConnection(sqliteConnString)) { conn.Open(); try { AddSQLiteView(conn, vs, viewFailureHandler); } catch (Exception ex) { _log.Error("AddSQLiteView failed", ex); throw; } } viewCount++; CheckCancelled(); progressReportingHandler(false, true, 50 + (int)(viewCount * 50.0 / schema.Views.Count), String.Format("Added view [{0}] to SQLite", vs.ViewName)); _log.Debug("added schema for SQLite view [" + vs.ViewName + "]"); } } _log.Debug("finished adding all table/view schemas for SQLite database"); }
/// <summary> /// This method takes as input the connection to an SQL Server database /// and creates a corresponding SQLite database file with a schema derived from /// the SQL Server database. /// </summary> /// <param name="sqlConnection">The SQL connection.</param> /// <param name="sqliteConnection">The SQLite connection.</param> /// <param name="sqlConversionHandler">The SQL conversion handler.</param> /// <param name="sqlTableSelectionHandler">The SQL table selection handler.</param> /// <param name="failedViewDefinitionHandler">The failed view definition handler.</param> /// <param name="createTriggers">if set to <c>true</c> [create triggers].</param> /// <remarks>The method continues asynchronously in the background and the caller returned /// immediatly.</remarks> public abstract void ConvertToDatabase(SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers);
private void btnStart_Click(object sender, EventArgs e) { string sqlConnString; if (cbxIntegrated.Checked) { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, (string)cboDatabases.SelectedItem); } else { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, (string)cboDatabases.SelectedItem, txtUserDB.Text, txtPassDB.Text); } bool createViews = cbxCreateViews.Checked; string sqlitePath = txtSQLitePath.Text.Trim(); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { UpdateSensitivity(); lblMessage.Text = msg; pbrProgress.Value = percent; if (done) { btnStart.Enabled = true; this.Cursor = Cursors.Default; UpdateSensitivity(); if (success) { MessageBox.Show(this, msg, "Conversion Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { if (!_shouldExit) { MessageBox.Show(this, msg, "Conversion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else Application.Exit(); } } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List<TableSchema> schema) { List<TableSchema> updated = null; Invoke(new MethodInvoker(delegate { // Allow the user to select which tables to include by showing him the // table selection dialog. TableSelectionDialog dlg = new TableSelectionDialog(); DialogResult res = dlg.ShowTables(schema, this); if (res == DialogResult.OK) updated = dlg.IncludedTables; })); return updated; }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { string updated = null; Invoke(new MethodInvoker(delegate { ViewFailureDialog dlg = new ViewFailureDialog(); dlg.View = vs; DialogResult res = dlg.ShowDialog(this); if (res == DialogResult.OK) updated = dlg.ViewSQL; else updated = null; })); return updated; }); string password = txtPassword.Text.Trim(); if (!cbxEncrypt.Checked) password = null; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, cbxTriggers.Checked, createViews); }
protected override void ConvertSourceDatabaseToDestination(SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = ReadSourceSchema(sqlConnection, sqlConversionHandler, sqlTableSelectionHandler); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqliteConnection, ds, sqlConversionHandler, failedViewDefinitionHandler); // Copy all rows from SQL Server tables to the newly created SQLite database CopySourceDatabaseRowsToDestination(sqlConnection, sqliteConnection, ds.Tables, sqlConversionHandler); // Add triggers based on foreign key constraints if (createTriggers) { AddTriggersForForeignKeys(sqliteConnection, ds.Tables, sqlConversionHandler); } }
static void Main(String[] args) { _options = new Options(); var result = CommandLine.Parser.Default.ParseArguments(args, _options); if (!result) { AddMessage("Invalid Arguments"); return; } String logFilePath = _options.LogFile; if (!String.IsNullOrWhiteSpace(logFilePath)) { if (File.Exists(logFilePath)) { File.Delete(logFilePath); } _logFileStream = new StreamWriter(File.OpenWrite(logFilePath)); } String filename = _options.ConfigFile; Boolean success = SerializationHelper.TryXmlDeserialize(filename, out _config); if (!success) { AddMessage("The selected file was not a valid configuration file for this application."); return; } if (!String.IsNullOrWhiteSpace(_options.DatabaseName)) { // Allow user to override database name. AddMessage(String.Format("A database name was supplied as an argument. Configured database will not be used.")); _config.DatabaseName = _options.DatabaseName; } AddMessage(String.Format("Converting database: {0}", _config.DatabaseName)); String sqlConnString = _config.ConnectionString; SqlConversionProgressReportingHandler progressReportingHandler = OnSqlConversionProgressReportingHandler; SqlTableSelectionHandler selectionHandlerDefinition = OnSqlTableDefinitionSelectionHandler; SqlTableSelectionHandler selectionHandlerRecords = OnSqlTableRecordSelectionHandler; FailedViewDefinitionHandler viewFailureHandler = OnFailedViewDefinitionHandler; var filePathWithReplacedEnvironmentValues = Environment.ExpandEnvironmentVariables(_config.SqLiteDatabaseFilePath); var task = SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, filePathWithReplacedEnvironmentValues, _config.EncryptionPassword, progressReportingHandler, selectionHandlerDefinition, selectionHandlerRecords, viewFailureHandler, _config.CreateTriggersEnforcingForeignKeys, _config.TryToCreateViews); task.Wait(); if (task.Exception != null) { AddMessage("An error has occurred. Details:"); var exception = task.Exception; AddMessage(exception.ToString(), false); foreach (var innerException in exception.InnerExceptions) { AddMessage(innerException.ToString(), false); } } if (_logFileStream != null) { _logFileStream.Dispose(); } }
protected override void ConvertSourceDatabaseToDestination(ConversionHandler conversionHandler, TableSelectionHandler tableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { throw new System.NotImplementedException(); }
private static void ConvertSqlServerDatabaseToSQLiteFile(string sqlConnString, string sqlitePath, string password, SqlConversionHandler handler, SqlTableSelectionHandler selectionHandler, FailedViewDefinitionHandler viewFailureHandler, bool createTriggers, bool createViews) { if (File.Exists(sqlitePath)) { File.Delete(sqlitePath); } DatabaseSchema databaseSchema = ReadSqlServerSchema(sqlConnString, handler, selectionHandler); CreateSQLiteDatabase(sqlitePath, databaseSchema, password, handler, viewFailureHandler, createViews); CopySqlServerRowsToSQLiteDB(sqlConnString, sqlitePath, databaseSchema.Tables, password, handler); if (createTriggers) { AddTriggersForForeignKeys(sqlitePath, databaseSchema.Tables, password, handler); } }
private void Iniciar() { string sqlConnString; sqlConnString = this.txtSqlServer.Text; bool createViews = cbxCreateViews.Checked; string sqlitePath = txtSQLite.Text.Trim(); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { UpdateSensitivity(); lblMessage.Text = msg; pbrProgress.Value = percent; if (done) { btnStart.Enabled = true; this.Cursor = Cursors.Default; UpdateSensitivity(); if (success) { pbrProgress.Value = 0; lblMessage.Text = string.Empty; Application.Exit(); } else { if (!_shouldExit) { MessageBox.Show(this, msg, "Conversion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else Application.Exit(); } } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List<TableSchema> schema) { List<TableSchema> updated = null; Invoke(new MethodInvoker(delegate { updated = schema; })); return updated; }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { string updated = null; Invoke(new MethodInvoker(delegate { ViewFailureDialog dlg = new ViewFailureDialog(); dlg.View = vs; DialogResult res = dlg.ShowDialog(this); if (res == DialogResult.OK) updated = dlg.ViewSQL; else updated = null; })); return updated; }); string password = txtPassword.Text.Trim(); if (!cbxEncrypt.Checked) password = null; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, cbxTriggers.Checked, createViews); }
/// <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="handler">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 ConvertOracleDatabaseToSQLiteFile( string sqlConnString, string sqlitePath, string password, SqlConversionHandler handler, List<String> includedTables, List<String> existedTables, FailedViewDefinitionHandler viewFailureHandler, bool createTriggers, bool createViews) { // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = ReadOracleSchema(sqlConnString, handler, includedTables, existedTables); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqlitePath, ds, password, handler, viewFailureHandler, createViews); // Copy all rows from SQL Server tables to the newly created SQLite database CopyOracleRowsToSQLiteDB(sqlConnString, sqlitePath, ds.Tables, password, handler); // Add triggers based on foreign key constraints if (createTriggers) AddTriggersForForeignKeys(sqlitePath, ds.Tables, password, handler); }
private void dbConvert_Load(object sender, EventArgs e) { string text = ""; string appSettings = ConfigOperation.GetAppSettings("OLD_POS_DATABASE_NAME"); text = ((!bool.Parse(ConfigOperation.GetAppSettings("OLD_POS_DATABASE_SSPI"))) ? $"Data Source=(local)\\SQLExpress;Initial Catalog={appSettings};User ID=sa;Password=1031" : $"Data Source=(local)\\SQLExpress;Initial Catalog={appSettings};Integrated Security=SSPI;"); string sqlitePath = Program.DataPath + "\\Old_db.db3"; Cursor = Cursors.WaitCursor; SqlConversionHandler handler = delegate(bool done, bool success, int percent, string msg) { dbConvert dbConvert = this; Invoke((MethodInvoker) delegate { dbConvert.lblMessage.Text = msg; dbConvert.pbrProgress.Value = percent; if (done) { dbConvert.Cursor = Cursors.Default; if (success) { MessageBox.Show(dbConvert, msg, "資料移轉成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); dbConvert.Close(); } else { MessageBox.Show(dbConvert, msg, "資料移轉失敗", MessageBoxButtons.OK, MessageBoxIcon.Hand); dbConvert.pbrProgress.Value = 0; dbConvert.lblMessage.Text = string.Empty; Application.Exit(); } } }); }; SqlTableSelectionHandler selectionHandler = null; FailedViewDefinitionHandler viewFailureHandler = delegate(ViewSchema vs) { dbConvert owner = this; string updated = null; Invoke((MethodInvoker) delegate { ViewFailureDialog viewFailureDialog = new ViewFailureDialog { View = vs }; if (viewFailureDialog.ShowDialog(owner) == DialogResult.OK) { updated = viewFailureDialog.ViewSQL; } else { updated = null; } }); return(updated); }; string password = "******"; bool createViews = false; bool createTriggers = false; SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(text, sqlitePath, password, handler, selectionHandler, viewFailureHandler, createTriggers, createViews); }
/// <summary> /// This method takes as input the connection string to an SQL Server database /// and creates a corresponding SQLite database file with a schema as retrieved from /// the SQL Server database. /// </summary> /// <param name="sqlServerConnString">The connection string to the SQL Server database.</param> /// <param name="sqlitePath">The path to the SQLite database file that needs to get created.</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 delegate for progress notifications.</param> /// <param name="selectionHandlerDefinition">The selection handler that allows the user to select which tables to include in the converted SQLite database.</param> /// /// <param name="selectionHandlerRecord">The selection handler that allows the user to select which tables to include the data of in the converted SQLite database.</param> /// <remarks>The method continues asynchronously in the background and the caller returns immediately.</remarks> public static Task ConvertSqlServerToSQLiteDatabase(string sqlServerConnString, string sqlitePath, string password, SqlConversionProgressReportingHandler progressReportingHandler, SqlTableSelectionHandler selectionHandlerDefinition, SqlTableSelectionHandler selectionHandlerRecord, FailedViewDefinitionHandler viewFailureHandler, Boolean createTriggers, Boolean createViews) { // Clear cancelled flag _cancelled = false; var task = Task.Factory.StartNew(() => { try { _isActive = true; String sqlitePathResolved = TemplateToFilename(sqlitePath); ConvertSqlServerDatabaseToSQLiteFile(sqlServerConnString, sqlitePathResolved, password, progressReportingHandler, selectionHandlerDefinition, selectionHandlerRecord, viewFailureHandler, createTriggers, createViews); _isActive = false; progressReportingHandler(true, true, 100, "Finished converting database"); } catch (Exception ex) { _log.Error("Failed to convert SQL Server database to SQLite database", ex); _isActive = false; progressReportingHandler(true, false, 100, ex.ToString()); } }); return(task); }
/// <summary> /// Creates the SQLite database. /// </summary> /// <param name="sqliteConnection">The SQLite connection.</param> /// <param name="schema">The schema.</param> /// <param name="handler">The handler.</param> /// <param name="viewFailureHandler">The view failure handler.</param> protected static void CreateSqlServerDatabase(SQLiteConnection sqliteConnection, DatabaseSchema schema, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler) { //Logging.Log(LogLevel.Debug, "Creating SQLite database..."); // Create all tables in the new database int count = 0; foreach (TableSchema dt in schema.Tables) { try { AddSQLiteTable(sqliteConnection, dt); } catch (Exception ex) { //Logging.Log(LogLevel.Error, string.Format("AddSQLiteTable failed: {0}", FileLogger.GetInnerException(ex).Message)); //Logging.HandleException(ex); throw; } count++; CheckCancelled(); handler(false, true, (int)(count * 50.0 / schema.Tables.Count), "Added table " + dt.TableName + " to the SQLite database"); //Logging.Log(LogLevel.Debug, "added schema for SQLite table [" + dt.TableName + "]"); } // Create all views in the new database count = 0; foreach (ViewSchema vs in schema.Views) { try { AddSQLiteView(sqliteConnection, vs, viewFailureHandler); } catch (Exception ex) { //Logging.Log(LogLevel.Error, string.Format("AddSQLiteView failed: {0}", FileLogger.GetInnerException(ex).Message)); //Logging.HandleException(ex); throw; } count++; CheckCancelled(); handler(false, true, 50 + (int)(count * 50.0 / schema.Views.Count), "Added view " + vs.ViewName + " to the SQLite database"); //Logging.Log(LogLevel.Debug, "added schema for SQLite view [" + vs.ViewName + "]"); } //Logging.Log(LogLevel.Debug, "finished adding all table/view schemas for SQLite database"); }
private void btnStart_Click(object sender, EventArgs e) { string sqlConnString; if (cbxIntegrated.Checked) { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, (string)cboDatabases.SelectedItem); } else { sqlConnString = GetSqlServerConnectionString(txtSqlAddress.Text, (string)cboDatabases.SelectedItem, txtUserDB.Text, txtPassDB.Text); } bool createViews = cbxCreateViews.Checked; string sqlitePath = txtSQLitePath.Text.Trim(); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { UpdateSensitivity(); lblMessage.Text = msg; pbrProgress.Value = percent; if (done) { btnStart.Enabled = true; this.Cursor = Cursors.Default; UpdateSensitivity(); if (success) { MessageBox.Show(this, msg, "Conversion Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { if (!_shouldExit) { MessageBox.Show(this, msg, "Conversion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { Application.Exit(); } } } })); }); SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List <TableSchema> schema) { List <TableSchema> updated = null; Invoke(new MethodInvoker(delegate { // Allow the user to select which tables to include by showing him the // table selection dialog. TableSelectionDialog dlg = new TableSelectionDialog(); DialogResult res = dlg.ShowTables(schema, this); if (res == DialogResult.OK) { updated = dlg.IncludedTables; } })); return(updated); }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { string updated = null; Invoke(new MethodInvoker(delegate { ViewFailureDialog dlg = new ViewFailureDialog(); dlg.View = vs; DialogResult res = dlg.ShowDialog(this); if (res == DialogResult.OK) { updated = dlg.ViewSQL; } else { updated = null; } })); return(updated); }); string password = txtPassword.Text.Trim(); if (!cbxEncrypt.Checked) { password = null; } SqlServerToSQLite.ConvertSqlServerToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, cbxTriggers.Checked, createViews); }
/// <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); } }
public static void ConvertSqlServerToSQLiteDatabase(string sqlServerConnString, string sqlitePath, string password, SqlConversionHandler handler, SqlTableSelectionHandler selectionHandler, FailedViewDefinitionHandler viewFailureHandler, bool createTriggers, bool createViews) { _cancelled = false; ThreadPool.QueueUserWorkItem(delegate { try { _isActive = true; ConvertSqlServerDatabaseToSQLiteFile(sqlServerConnString, sqlitePath, password, handler, selectionHandler, viewFailureHandler, createTriggers, createViews); _isActive = false; handler(done: true, success: true, 100, "資料移轉成功"); } catch (SqlException exception) { _log.Error("資料移轉失敗", exception); _isActive = false; handler(done: true, success: false, 100, "查無原先POS系統資料庫,請洽客服人員"); } catch (Exception ex) { _log.Error("資料移轉失敗", ex); _isActive = false; handler(done: true, success: false, 100, ex.Message); } }); }
/// <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="sqlConnection">The SQL connection.</param> /// <param name="sqliteConnection">The SQLite connection.</param> /// <param name="sqlConversionHandler">The SQL conversion handler.</param> /// <param name="sqlTableSelectionHandler">The SQL table selection handler.</param> /// <param name="failedViewDefinitionHandler">The failed view definition handler.</param> /// <param name="createTriggers">if set to <c>true</c> [create triggers].</param> protected abstract void ConvertSourceDatabaseToDestination(SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers);
public override void ConvertToDatabase(ConversionHandler conversionHandler, TableSelectionHandler tableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { using (sqlConnection = new SqlConnection(Connections.OtherConnection.ConnectionString)) { try { sqlConnection.Open(); LoadSchema(); sqlConnection.Close(); } catch (Exception) { if (sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); } throw; } } }
/// <summary> /// This method takes as input the connection string to an SQL Server database /// and creates a corresponding SQLite database file with a schema derived from /// the SQL Server database. /// </summary> /// <param name="sqlServerConnectionString">The connection string to the SQL Server database.</param> /// <param name="sqliteConnectionString">The connection string to the SQLite database.</param> /// <param name="sqlConversionHandler">The SQL conversion handler.</param> /// <param name="sqlTableSelecttionHandler">The SQL table selection handler.</param> /// <param name="failedViewDefinitionHandler">The failed view definition handler.</param> /// <param name="createTriggers">if set to <c>true</c> [create triggers].</param> /// <remarks>The method continues asynchronously in the background and the caller returned /// immediatly.</remarks> public abstract void ConvertToDatabase(string sqlServerConnectionString, string sqliteConnectionString, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers);
/// <summary> /// Creates the SQLite database from the schema read from the SQL Server. /// </summary> /// <param name="sqlitePath">The path to the generated DB file.</param> /// <param name="schema">The schema of the SQL server database.</param> /// <param name="password">The password to use for encrypting the DB or null if non is needed.</param> /// <param name="handler">A handle for progress notifications.</param> private static void CreateSQLiteDatabase(string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler) { _log.Debug("Creating SQLite database..."); // Create the SQLite database file SQLiteConnection.CreateFile(sqlitePath); _log.Debug("SQLite file was created successfully at [" + sqlitePath + "]"); // Connect to the newly created database string sqliteConnString = CreateSQLiteConnectionString(sqlitePath, password); using (SQLiteConnection conn = new SQLiteConnection(sqliteConnString)) { conn.Open(); // Create all tables in the new database int count = 0; foreach (TableSchema dt in schema.Tables) { try { AddSQLiteTable(conn, dt); } catch (Exception ex) { _log.Error("AddSQLiteTable failed", ex); throw; } count++; CheckCancelled(); handler(false, true, (int)(count * 50.0 / schema.Tables.Count), "Added table " + dt.TableName + " to the SQLite database"); _log.Debug("added schema for SQLite table [" + dt.TableName + "]"); } // foreach // Create all views in the new database count = 0; foreach (ViewSchema vs in schema.Views) { try { AddSQLiteView(conn, vs, viewFailureHandler); } catch (Exception ex) { _log.Error("AddSQLiteView failed", ex); throw; } // catch count++; CheckCancelled(); handler(false, true, 50+(int)(count * 50.0 / schema.Views.Count), "Added view " + vs.ViewName + " to the SQLite database"); _log.Debug("added schema for SQLite view [" + vs.ViewName + "]"); } // foreach } // using _log.Debug("finished adding all table/view schemas for SQLite database"); }
private static void AddSQLiteView(SQLiteConnection conn, ViewSchema vs, FailedViewDefinitionHandler handler) { // Prepare a CREATE VIEW DDL statement string stmt = vs.ViewSQL; _log.Info("\n\n" + stmt + "\n\n"); // Execute the query in order to actually create the view. SQLiteTransaction tx = conn.BeginTransaction(); try { SQLiteCommand cmd = new SQLiteCommand(stmt, conn, tx); cmd.ExecuteNonQuery(); tx.Commit(); } catch (SQLiteException ex) { tx.Rollback(); if (handler != null) { ViewSchema updated = new ViewSchema(); updated.ViewName = vs.ViewName; updated.ViewSQL = vs.ViewSQL; // Ask the user to supply the new view definition SQL statement string sql = handler(updated); if (sql == null) return; // Discard the view else { // Try to re-create the view with the user-supplied view definition SQL updated.ViewSQL = sql; AddSQLiteView(conn, updated, handler); } } else throw; } // catch }
private static void CreateSQLiteDatabase(string sqlitePath, DatabaseSchema schema, string password, SqlConversionHandler handler, FailedViewDefinitionHandler viewFailureHandler, bool createViews) { _log.Debug("Creating SQLite database..."); SQLiteConnection.CreateFile(sqlitePath); _log.Debug("SQLite file was created successfully at [" + sqlitePath + "]"); using (SQLiteConnection sQLiteConnection = new SQLiteConnection(CreateSQLiteConnectionString(sqlitePath, password))) { sQLiteConnection.Open(); int num = 0; foreach (TableSchema table in schema.Tables) { try { AddSQLiteTable(sQLiteConnection, table); } catch (Exception exception) { _log.Error("AddSQLiteTable failed", exception); throw; } num++; CheckCancelled(); handler(done: false, success: true, (int)((double)num * 50.0 / (double)schema.Tables.Count), "產生資料表: " + table.TableName); _log.Debug("added schema for SQLite table [" + table.TableName + "]"); } num = 0; if (createViews) { foreach (ViewSchema view in schema.Views) { try { AddSQLiteView(sQLiteConnection, view, viewFailureHandler); } catch (Exception exception2) { _log.Error("AddSQLiteView failed", exception2); throw; } num++; CheckCancelled(); handler(done: false, success: true, 50 + (int)((double)num * 50.0 / (double)schema.Views.Count), "產生view: " + view.ViewName); _log.Debug("added schema for SQLite view [" + view.ViewName + "]"); } } } _log.Debug("finished adding all table/view schemas for SQLite database"); }
protected override void ConvertSourceDatabaseToDestination(SqlConnection sqlConnection, SQLiteConnection sqliteConnection, SqlConversionHandler sqlConversionHandler, SqlTableSelectionHandler sqlTableSelectionHandler, FailedViewDefinitionHandler failedViewDefinitionHandler, bool createTriggers) { // Read the schema of the SQL Server database into a memory structure DatabaseSchema ds = ReadSourceSchema(sqlConnection, sqlConversionHandler, sqlTableSelectionHandler); // Create the SQLite database and apply the schema CreateSQLiteDatabase(sqliteConnection, ds, sqlConversionHandler, failedViewDefinitionHandler); // Copy all rows from SQL Server tables to the newly created SQLite database CopySourceDatabaseRowsToDestination(sqlConnection, sqliteConnection, ds.Tables, sqlConversionHandler); // Add triggers based on foreign key constraints if (createTriggers) AddTriggersForForeignKeys(sqliteConnection, ds.Tables, sqlConversionHandler); }
/// <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); } }
private void btnStart_Click(object sender, EventArgs e) { string sqlConnString = GetSqlServerConnectionString(); bool createViews = cbxCreateViews.Checked; string sqlitePath = txtSQLitePath.Text.Trim(); this.Cursor = Cursors.WaitCursor; SqlConversionHandler handler = new SqlConversionHandler(delegate(bool done, bool success, int percent, string msg) { Invoke(new MethodInvoker(delegate() { UpdateSensitivity(); lblMessage.Text = msg; pbrProgress.Value = percent; if (done) { btnStart.Enabled = true; this.Cursor = Cursors.Default; UpdateSensitivity(); if (success) { MessageBox.Show(this, msg, "Conversion Finished", MessageBoxButtons.OK, MessageBoxIcon.Information); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else { if (!_shouldExit) { MessageBox.Show(this, msg, "Conversion Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); pbrProgress.Value = 0; lblMessage.Text = string.Empty; } else Application.Exit(); } } })); }); // SqlTableSelectionHandler selectionHandler = new SqlTableSelectionHandler(delegate(List<string> schema, List<int> numRows) // { // return IncludedTables; // }); FailedViewDefinitionHandler viewFailureHandler = new FailedViewDefinitionHandler(delegate(ViewSchema vs) { string updated = null; Invoke(new MethodInvoker(delegate { ViewFailureDialog dlg = new ViewFailureDialog(); dlg.View = vs; DialogResult res = dlg.ShowDialog(this); if (res == DialogResult.OK) updated = dlg.ViewSQL; else updated = null; })); return updated; }); string password = txtPassword.Text.Trim(); if (!cbxEncrypt.Checked) password = null; List<string> includedTables = new List<string>(); List<string> existedTables = new List<string>(); foreach (DataGridViewRow row in dataGridView1.Rows) { bool include = (bool)row.Cells[0].Value; if (include) { includedTables.Add((string)row.Cells[1].Value); if ((string)row.Cells[5].Value == "Existed") existedTables.Add((string)row.Cells[1].Value); } } // foreach OracleToSQLite.ConvertOracleToSQLiteDatabase(sqlConnString, sqlitePath, password, handler, includedTables, existedTables, viewFailureHandler, cbxTriggers.Checked, createViews); }