Click here to Skip to main content
11,432,168 members (62,599 online)
Click here to Skip to main content

Convert SQL Server DB to SQLite DB

, 13 Jan 2013 Public Domain
Rate this:
Please Sign up or sign in to vote.
A C# utility to automatically do conversion from a SQL Server DB to a SQLite DB
convsqlservertosqlite/converter.png

Introduction

I needed to convert the existing SQL server databases to SQLite databases as part of a DB migration program and did not find any decent free converter to do the job.

This is my attempt to solve the problem. I should warn you though that I did not have much time to test it on too many databases. In any case - the source code is very well documented and easy to understand, so if you do have a problem it should be relatively easy to fix. (Please send me the fixed source code. If you do so, I can update the software so that everybody can enjoy it.)

Using the Code

The code is split between a dataaccess project (class library) that contains the conversion code itself and a converter project (WinForms) that drives the conversion code and provides a simple UI for user interaction.

The main class that performs the conversion is the sqlservertosqlite class. It does the conversion by doing the following steps:

  1. Reading the designated SQL server schema and preparing a list of tableschema objects that contain the schema for each and every SQL server table (and associated structures like indexes).
  2. Preparing an empty sqlite database file with the schema that was read from SQL server. In this step, the code may alter few SQL-server types that are not supported directly in sqlite.
  3. Copying rows for each table from the SQL server database to the sqlite database.

Basically, that's it!

Points of Interest

In order to read the SQL server DB schema, I was mainly using the pseudo information_schema.table table. You can find more information about it on the Internet if you want.

History

  • 13th June, 2008: Initial version
  • 08th July, 2008: v 1.2
    • Fixed a bug that caused unique indexes to be generated as non-unique in some cases
  • 08th July, 2008: v 1.3
    • Fixed a bug that caused the utility to crash sometimes when processing index information
  • 17th July, 2008: v 1.4
    • Fixed a bug that caused wrong columns to become primary keys on rare occasions and improved conversion performance
  • 20th July, 2008: v 1.5
    • Added support for case-insensitive columns (COLLATE NOCASE)
  • 22nd July, 2008: v 1.6
    • Added support for encrypting the resulting DB file (using the built-in encryption support that exists in the SQLite .NET provider)
  • 05th October, 2008: v 1.7
    • Fixed information_schema references to use UPPER-CASE in order to resolve international character set issues (Turkish)
  • 14th December 2008: v1.8
    • Integrated support for foreign keys from the revised version made by Yogesh Jagota
    • Merged support for selective table import
  • 21st February 2009: v1.9
    • Added contribution from johnny dickson cano that allows to select using SQL server integrated security or using user name /password instead
    • Added support for converting IDENTITY columns to AUTOINCREMENT in SQLite (suggestion by Paul Shaffer)
  • 04th March 2009: v1.10
    • Fixed a bug that caused the converter to crash when encountering a datetime field in the original SQL server schema. Thanks to bmcclint for sending me the bug with the correct bugfix.
  • 23rd May 2009: v1.11
    • Added support for simulating foreign keys using triggers (contributed by Martijn Muurman)
    • Added a small bugfix so that now an 'int' type is always converted to 'integer' type in sqlite. This was needed because sqlite will autoincrement only on 'integer' column types.
  • 04th June 2009: v1.12
    • Fixed a bug in trigger generation code that caused schema generation to fail when more than a single column is referencing the same column in a foreign table
  • 20th September 2009: v1.13
    • Fixed AUTOINCREMENT bug suggested by MAEP
    • Fixed 64 bit support problem (thanks to Murry Gammash)
    • Added support for converting SQL Server views (suggested by Richard Thurgood)
  • 22nd September 2009: v1.14
    • Fixed a critical bug that caused the conversion process to fail on some SQL Server databases that used the [dbo] notation.
  • 25th September 2009: v1.15
    • Fixed a critical bug that caused the primary keys to be discarded
    • Fixed trigger generation bug
  • 4th December 2009: v1.16
    • Fixed generation code to create GUID types for SQL-Server's uniqueidentifier type (instead of nvarchar as it was until now)
    • Updated the solution to use the latest SQLite .NET provider library
  • 24th March, 2011
    • Attached compiled version of the project for anyone that doesn't have Visual Studio and still needs to use the utility
  • 1st July, 2011
    • Updated binary zip as the earlier one was missing the DLL file needed by the application
  • 15th Nov 2011: v1.17
    • Fixed a bug that caused the software to crash when encountering NULL values in some of SQL Server meta data tables
  • 19th June 2012: v1.19
    • Added support for ignoring views when creating the DB schema, Added support for blank characters inside column names.
  •  14th January 2013: v1.20
    • Fixed problem with column names
    • Added more width to the database names combo box. 

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

liron.levi
Software Developer Arineta Cardio Imaging
Israel Israel
My name is Liron Levi and I'm developing software for fun & profit for 15 years already. I'm now working for Arineta Cardio Imaging as a software developer (the company develops a CT machine).

I can be contacted directly at liron.levi@outlook.com or via linkedin at http://www.linkedin.com/pub/liron-levy/1/578/ab5

Comments and Discussions

 
QuestionComplied CommandLine version [modified] Pin
Member 88087012-Jul-13 9:42
memberMember 88087012-Jul-13 9:42 
QuestionCascade delete Pin
VeereshKumar8-Jul-13 12:43
memberVeereshKumar8-Jul-13 12:43 
AnswerRe: Cascade delete Pin
liron.levi8-Jul-13 21:21
memberliron.levi8-Jul-13 21:21 
QuestionCan not connect - help me please. Pin
iTimeLiGhT5-Jul-13 13:56
memberiTimeLiGhT5-Jul-13 13:56 
AnswerRe: Can not connect - help me please. Pin
liron.levi6-Jul-13 6:51
memberliron.levi6-Jul-13 6:51 
GeneralRe: Can not connect - help me please. Pin
iTimeLiGhT6-Jul-13 8:31
memberiTimeLiGhT6-Jul-13 8:31 
GeneralRe: Can not connect - help me please. Pin
liron.levi6-Jul-13 9:07
memberliron.levi6-Jul-13 9:07 
GeneralRe: Can not connect - help me please. Pin
iTimeLiGhT9-Jul-13 3:32
memberiTimeLiGhT9-Jul-13 3:32 
GeneralRe: Can not connect - help me please. Pin
liron.levi9-Jul-13 5:21
memberliron.levi9-Jul-13 5:21 
GeneralRe: Can not connect - help me please. Pin
iTimeLiGhT9-Jul-13 9:05
memberiTimeLiGhT9-Jul-13 9:05 
GeneralRe: Can not connect - help me please. Pin
liron.levi9-Jul-13 10:59
memberliron.levi9-Jul-13 10:59 
GeneralExcellent work - many thanks Pin
Member 42194415-May-13 0:27
memberMember 42194415-May-13 0:27 
GeneralRe: Excellent work - many thanks Pin
liron.levi15-May-13 8:25
memberliron.levi15-May-13 8:25 
QuestionIndex sort order Pin
Member 100381268-May-13 12:55
memberMember 100381268-May-13 12:55 
Hi,

Thanks for a great tool!
I have not used it since version 1.16. Now when upgrading to 1.20 I have a problem with interpretation of indexes. The indexes found in SQL server are converted to descending although they are ascending in SQL server.

Have you seen this behaviour?

Thanks,

Ola Lov
AnswerRe: Index sort order Pin
liron.levi8-May-13 21:02
memberliron.levi8-May-13 21:02 
GeneralMy vote of 5 Pin
fengfranklu2-May-13 10:30
memberfengfranklu2-May-13 10:30 
Questionsuper man Pin
toin lee14-Apr-13 23:23
membertoin lee14-Apr-13 23:23 
AnswerRe: super man Pin
liron.levi15-Apr-13 0:12
memberliron.levi15-Apr-13 0:12 
QuestionFew question about conversion Pin
Tridip Bhattacharjee10-Apr-13 22:09
memberTridip Bhattacharjee10-Apr-13 22:09 
AnswerRe: Few question about conversion Pin
liron.levi11-Apr-13 1:36
memberliron.levi11-Apr-13 1:36 
GeneralRe: Few question about conversion Pin
Tridip Bhattacharjee11-Apr-13 10:07
memberTridip Bhattacharjee11-Apr-13 10:07 
GeneralRe: Few question about conversion Pin
liron.levi11-Apr-13 20:43
memberliron.levi11-Apr-13 20:43 
GeneralRe: Few question about conversion Pin
Tridip Bhattacharjee11-Apr-13 21:38
memberTridip Bhattacharjee11-Apr-13 21:38 
QuestionMy vote of 5 Pin
frazzle-me4-Apr-13 10:19
memberfrazzle-me4-Apr-13 10:19 
AnswerRe: My vote of 5 Pin
liron.levi5-Apr-13 0:00
memberliron.levi5-Apr-13 0:00 
GeneralMy vote of 5 Pin
Member 805299226-Mar-13 1:04
memberMember 805299226-Mar-13 1:04 
QuestionDatabase Encoding Pin
Rafael Vetrone20-Mar-13 5:34
memberRafael Vetrone20-Mar-13 5:34 
AnswerRe: Database Encoding Pin
liron.levi20-Mar-13 6:54
memberliron.levi20-Mar-13 6:54 
GeneralRe: Database Encoding Pin
Rafael Vetrone21-Mar-13 10:02
memberRafael Vetrone21-Mar-13 10:02 
Questionsuperb program :) Pin
James loong10-Mar-13 18:54
memberJames loong10-Mar-13 18:54 
AnswerRe: superb program :) Pin
liron.levi10-Mar-13 22:09
memberliron.levi10-Mar-13 22:09 
Answergood Pin
Member 15605935-Feb-13 22:32
memberMember 15605935-Feb-13 22:32 
QuestionGreat Pin
Win32nipuh23-Jan-13 5:22
memberWin32nipuh23-Jan-13 5:22 
AnswerRe: Great Pin
liron.levi26-Jan-13 20:53
memberliron.levi26-Jan-13 20:53 
GeneralMy vote of 5 Pin
Edo Tzumer14-Jan-13 2:42
memberEdo Tzumer14-Jan-13 2:42 
GeneralRe: My vote of 5 Pin
liron.levi14-Jan-13 5:12
memberliron.levi14-Jan-13 5:12 
QuestionSelect DB Dropdown List Width Pin
Member 975255913-Jan-13 7:58
memberMember 975255913-Jan-13 7:58 
AnswerRe: Select DB Dropdown List Width Pin
liron.levi13-Jan-13 21:07
memberliron.levi13-Jan-13 21:07 
GeneralRe: Select DB Dropdown List Width Pin
Member 975255914-Jan-13 17:02
memberMember 975255914-Jan-13 17:02 
QuestionGreat!! Pin
Member 358483425-Dec-12 21:06
memberMember 358483425-Dec-12 21:06 
QuestionThanks.. Great. Pin
jutatip16-Dec-12 18:38
memberjutatip16-Dec-12 18:38 
QuestionForeign Key mismatch Problems Pin
sekerg16-Dec-12 7:21
membersekerg16-Dec-12 7:21 
QuestionGreat! Pin
Member 770135722-Nov-12 23:02
memberMember 770135722-Nov-12 23:02 
AnswerRe: Great! Pin
liron.levi23-Nov-12 4:49
memberliron.levi23-Nov-12 4:49 
Questionunsuppoerted file error while connecting ? Pin
oguzkaygun26-Oct-12 9:20
memberoguzkaygun26-Oct-12 9:20 
AnswerRe: unsuppoerted file error while connecting ? Pin
liron.levi26-Oct-12 22:52
memberliron.levi26-Oct-12 22:52 
QuestionThank you!! Pin
Rablinz1-Oct-12 1:46
memberRablinz1-Oct-12 1:46 
BugSmall DDL error in Column Names? Pin
Tomtommit29-Sep-12 6:35
memberTomtommit29-Sep-12 6:35 
GeneralRe: Small DDL error in Column Names? Pin
liron.levi29-Sep-12 8:14
memberliron.levi29-Sep-12 8:14 
QuestionWhat to put in "SQL server address" ? Pin
Alaeddine Ghribi27-Sep-12 13:10
memberAlaeddine Ghribi27-Sep-12 13:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 14 Jan 2013
Article Copyright 2008 by liron.levi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid