Click here to Skip to main content
6,593,923 members and growing! (13,599 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

A C# utility to automatically do conversion from a SQL Server DB to a SQLite DB
C#.NET 2.0SQL 2000, SQL 2005, DBA, Dev
Version:9 (See All)
Posted:13 Jun 2008
Updated:26 Sep 2009
Views:52,727
Bookmarked:136 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
44 votes for this article.
Popularity: 7.89 Rating: 4.80 out of 5

1

2
2 votes, 4.5%
3
4 votes, 9.1%
4
38 votes, 86.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
  • 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

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.

I'm also selling a small utility for comparing and merging SQLite databases on http://www.sqlitecompare.com

I can be contacted directly at liron.levi@gmail.com
Occupation: Software Developer
Company: Green Pine Software Ltd.
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 117 (Total in Forum: 117) (Refresh)FirstPrevNext
GeneralProblem with attached DB Pinmemberleonen9:54 2 Oct '09  
GeneralRe: Problem with attached DB Pinmemberliron.levi11:26 2 Oct '09  
Generalthank you PinmemberDadou00218:40 28 Sep '09  
QuestionError message in Visual Studio when adding tables to dataset Pinmemberjoepaulsson2:56 23 Sep '09  
AnswerRe: Error message in Visual Studio when adding tables to dataset Pinmemberliron.levi4:10 23 Sep '09  
GeneralSQLServer to MySQL is wanted PinmemberXiaosheng Wang3:54 22 Sep '09  
GeneralRe: SQLServer to MySQL is wanted Pinmemberliron.levi5:38 22 Sep '09  
GeneralCouldn't understand what did you change? Pinmemberliron.levi7:50 19 Sep '09  
GeneralRe: Couldn't understand what did you change? Pinmemberzystory16:32 21 Sep '09  
GeneralRe: Couldn't understand what did you change? Pinmemberzystory16:36 21 Sep '09  
GeneralRe: Couldn't understand what did you change? Pinmemberzystory19:43 21 Sep '09  
GeneralThanks a lot, great tool PinmemberMAEP13:58 18 Aug '09  
GeneralRe: Thanks a lot, great tool Pinmemberliron.levi21:33 18 Aug '09  
GeneralRe: Thanks a lot, great tool Pinmemberliron.levi11:49 20 Sep '09  
GeneralMigrating views PinmemberElGuroProgramador23:45 2 Aug '09  
GeneralRe: Migrating views Pinmemberliron.levi23:55 2 Aug '09  
GeneralRe: Migrating views PinmemberMember 309062213:00 3 Sep '09  
GeneralRe: Migrating views Pinmemberliron.levi1:50 4 Sep '09  
GeneralRe: Migrating views PinmemberMember 30906223:17 4 Sep '09  
GeneralRe: Migrating views Pinmemberliron.levi8:49 4 Sep '09  
GeneralRe: Migrating views Pinmemberliron.levi11:53 20 Sep '09  
GeneralRe: Migrating views PinmemberRichard.Thurgood14:06 20 Sep '09  
GeneralINTEGER PRIMARY KEY AUTOINCREMENT fix PinmemberElGuroProgramador22:13 2 Aug '09  
GeneralRe: INTEGER PRIMARY KEY AUTOINCREMENT fix Pinmemberliron.levi22:53 2 Aug '09  
GeneralRe: INTEGER PRIMARY KEY AUTOINCREMENT fix PinmemberElGuroProgramador23:18 2 Aug '09  

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

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