Created
May 4, 2017 20:38
-
-
Save jbe2277/21ed579314b5d17b034fa32e75796d82 to your computer and use it in GitHub Desktop.
Convert SQL Compact Edition database file to SQLite database file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// === Convert SQL Compact Edition database file to SQLite database file === | |
// Dump SQL Compact Edition data to SQLite compatible SQL file | |
// - Command line tools: https://github.com/ErikEJ/SqlCeToolbox/wiki/Command-line-tools | |
// - ExportSQLCE40.exe "Data Source=D:\Northwind.sdf;" Northwind.sql sqlite | |
// The SQL file created by the SqlCeToolbox contains GUID data as text. | |
// - Connection string with GUID as text: https://www.connectionstrings.com/sqlite/ | |
// Data Source=c:\mydb.db;Version=3;BinaryGUID=False; | |
// - Recommended is to store GUID data as binary instead of text (requires less space) | |
// Use the C# command line application posted below to convert the GUIDs within the SQL file from text to binary | |
// Create new SQLite database file from SQL file | |
// - sqlite3.exe some.db < data.sql | |
internal class Program | |
{ | |
internal static void Main(string[] args) | |
{ | |
Optimize(args[0], args[1]); | |
Console.ReadLine(); | |
} | |
internal static void Optimize(string sourceFile, string targetFile) | |
{ | |
Console.WriteLine($"Source: {sourceFile}"); | |
Console.WriteLine($"Target: {targetFile}"); | |
var lines = File.ReadAllLines(sourceFile); | |
for (int i = 0; i < lines.Length; i++) | |
{ | |
var parts = lines[i].Split(','); | |
for (int j = 0; j < parts.Length; j++) | |
{ | |
bool last = parts[j].EndsWith(");"); | |
Guid guid; | |
var guidPart = parts[j]; | |
if (last) | |
{ | |
guidPart = guidPart.Substring(0, guidPart.Length - 2); | |
} | |
if (Guid.TryParseExact(guidPart.Trim('\''), "D", out guid)) | |
{ | |
parts[j] = string.Format("x'{0}'", BitConverter.ToString(guid.ToByteArray()).Replace("-", "")) + (last ? ");" : ""); | |
} | |
} | |
lines[i] = string.Join(",", parts); | |
Console.WriteLine(lines[i]); | |
} | |
File.WriteAllLines(targetFile, lines); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment