Skip to content

Instantly share code, notes, and snippets.

@amjad
Forked from jbe2277/OptimizeSqlFile.cs
Created June 16, 2018 19:49
Show Gist options
  • Save amjad/11a64529ff7793f1bb610213fbf25bb8 to your computer and use it in GitHub Desktop.
Save amjad/11a64529ff7793f1bb610213fbf25bb8 to your computer and use it in GitHub Desktop.
Convert SQL Compact Edition database file to SQLite database file
// === 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