Click here to Skip to main content
6,291,124 members and growing! (17,085 online)
Email Password   helpLost your password?
Database » Database » Utilities     Intermediate License: A Public Domain dedication

Convert SQL Server DB to SQLite DB

By liron.levi

C# utility to automatically do the conversion from SQL Server DB to SQLite DB
C#, .NET (.NET 2.0), SQL Server (SQL 2000, SQL 2005), DBA, Dev
Version:6 (See All)
Posted:13 Jun 2008
Updated:5 Jun 2009
Views:37,157
Bookmarked:107 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
34 votes for this article.
Popularity: 7.27 Rating: 4.75 out of 5

1

2
2 votes, 5.9%
3
4 votes, 11.8%
4
28 votes, 82.4%
5
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

License

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

About the Author

liron.levi


Member
My name is Liron Levi and I'm developing software for fun & profit for 15 years already. I own a small consulting business in Kiryat Tivon / ISRAEL.

My areas of expertise are:

.NET software development
Command and control software systems (C2, C4I)
GUI and usability design
Software Requirements
Software Architecture
Networking (TCP/IP)

I can be contacted directly at liron.levi@gmail.com
Occupation: Software Developer
Company: Dev-Exp consulting
Location: Israel Israel

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 81 (Total in Forum: 81) (Refresh)FirstPrevNext
Generala great job for me.. thanks a lot Pinmemberviv_wangsc19:10 26 Jun '09  
GeneralRe: a great job for me.. thanks a lot Pinmemberliron.levi22:21 26 Jun '09  
GeneralVery useful Tools PinmemberFrank.Zhu19:05 15 Jun '09  
GeneralRe: Very useful Tools Pinmemberliron.levi20:35 15 Jun '09  
GeneralGood job! PinmemberBobcsi23:43 10 Jun '09  
GeneralFound bugs, but great soft anyway!!! Pinmembersuper_beo14:27 29 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi21:33 29 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo13:58 30 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi21:11 30 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo4:37 31 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi4:42 31 May '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo4:35 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi4:43 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo4:57 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi5:09 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo5:11 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmemberliron.levi21:36 4 Jun '09  
GeneralRe: Found bugs, but great soft anyway!!! Pinmembersuper_beo6:46 5 Jun '09  
GeneralTriggers for foreign keys Pinmembergluip11:30 13 May '09  
GeneralRe: Triggers for foreign keys Pinmemberliron.levi10:28 14 May '09  
GeneralParent Columns and Child Columns don't have type-matching columns PinmemberMember 86070917:03 25 Apr '09  
GeneralRe: Parent Columns and Child Columns don't have type-matching columns Pinmemberliron.levi22:23 25 Apr '09  
GeneralMissing IDENTITY conversion to AUTOINCREMENT [modified] Pinmemberlinenoise887:34 16 Mar '09  
GeneralRe: Missing IDENTITY conversion to AUTOINCREMENT Pinmemberliron.levi10:40 16 Mar '09  
NewsIllegal DB type found (datetime) Pinmemberbmcclint9:33 4 Mar '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jun 2009
Editor: Deeksha Shenoy
Copyright 2008 by liron.levi
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project