Click here to Skip to main content
Click here to Skip to main content

JET to SQL Converter

By , 31 Aug 2003
 

Introduction

To help with the deployment of a database project, I have chosen to embed a blank Access database into the setup program.  The tables will then be added by calling SQL statements on the database.  I chose this course for two reasons:

  1. The database is still in heavy production.  Committing each change to CVS is blowing out the size of the repository.  Instead I can just save the SQL commands as a single file.
  2. If I need to upgrade the database in a production setting, I can diff between versions and can call ALTER TABLE statements on the existing database as part of the upgrade setup.

I still find it easier to use Access to design my database.  I searched the internet for a tool that could automate the transformation of the database to SQL statements.  When I couldn't find anything suitable, I decided to write my own.

Background

I chose to write the module in C# (I am trying to learn it at the moment).  It uses the OleDbConnection.GetOleDbSchemaTable command to extract schema information from the database, then parses the information to give a collection of 'Tables'.  These Tables are then written out in SQL commands.

The only major problem that I have found is that I could not identify whether a column was AutoNumber or not from the schema information returned.  I have assumed that any Primary Key with an Integer data type is an AutoNumber.

Since the module was written primarily for my own use, I have really only checked that it works with my database.  I also have successfully read and written the Northwind database.  If other information is required, have a look at the information returned by the various OleDbSchemaGuid values.

The code still requires a lot of work on the error/exception handling side of things.  It shouldn't effect the database you are running it against, but use it at your own risk!

Using the code

The class SQLWriter contains a single public method, GetSQLString().  To use the library, simply instantiate the class by passing the filename of the Access database, then call GetSQLString().  This function returns a string of the SQL commands needed to create the database tables.

For example:

// An example of using GetSQLString()
using JetDBReader;

class ConsoleApp {
  public static void Main(string[] args)
  {
    SQLWriter cl = new SQLWriter (@"c:\databases\northwind.mdb");
    
    Console.WriteLine(cl.GetSQLString());
  }
}

The class also uses a separate library, AlgorithmLib, that I have started.  At the moment it only contains a single function, TopologicalSort (probably highly unoptimised), but I hope to add any generic algorithms as I need them.

The example project includes a very simple WinForms project that basically allows you to choose an input file and output file.  No verification or exception handling is included.

Points of Interest

Coming from a total non-CS background (I am a Civil Engineer by trade), there are likely to be a lot of stylistic and syntactic problems with the code.  I am really interested in people's comments as to how I can improve my coding and design skills, hopefully so, by the time the next dot-com boom comes around, I might be up to a reasonable standard.  Designing code beats designing sewer systems any day!

History

  • Version 0.1: First upload.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rowdy75
Engineer
Australia Australia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDetermine wether a field is an autonumber fieldmemberfantomas6231 Aug '06 - 1:00 
Frans Bouma give a solution to that.


 
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [" +
tableName + "] WHERE 1=0", openConnection);
 
DataTable tableSchema = adapter.FillSchema(new DataTable(),
SchemaType.Source);
 
for (int j = 0; j < tableSchema.Columns.Count; j++)
{
if(tableSchema.Columns[j].AutoIncrement)
{
// is identity
}
}
 

Source : http://www.pcreview.co.uk/forums/thread-1248566.php[^]
GeneralWow... a real fast plunge into .NETmembergnatko14 Jan '04 - 19:23 
Believe it or not, early this morning I knew nothing about .NET... I just needed something to covert the structure of an .MDE to MS SQL. I downloaded this tool and it didn't work. This is the way I was driven to download the .NET framework 1.1 and now the tool works fine! Kinda fast migration to .NET)) I'm downloading the SDK now.
 
There was only one problem that I figured out: my .MDE file has tables and fields named in Russian. sqljet writes them down in Unicode, which is unreadable in a plain text file. In Russia, we usually convert OLE strings (where Russian chars are in Unicode double-char) to single-byte char strings when exporting somewhere. You could know that, anyway.
 
Generally, thanks for a quick solution to my problem and a good start with .NET!
GeneralUseful code...memberPaul Evans8 Dec '03 - 23:46 
but where's the article?
 
/**********************************
Paul Evans, Dorset, UK.
Personal Homepage "EnjoySoftware" @
http://www.enjoysoftware.co.uk/
**********************************/

GeneralConformance to the Microsoft .NET Framework Design Guidelinesmember//\('')/\\3 Sep '03 - 8:30 
FxCop is a code analysis tool that checks .NET managed code assemblies for conformance to the Microsoft .NET Framework Design Guidelines. It uses reflection, MSIL parsing, and callgraph analysis to inspect assemblies for more than 200 defects in the following areas: naming conventions, library design, localization, security, and performance.
 
First of all you can use it to "clean up" your code.Smile | :)
GeneralRe: Conformance to the Microsoft .NET Framework Design Guidelinesmember//\('')/\\3 Sep '03 - 8:31 
You can download the latest version at:
http://www.gotdotnet.com/team/fxcop/
GeneralRe: Conformance to the Microsoft .NET Framework Design GuidelinesmemberPaul Evans8 Dec '03 - 23:45 
I downloaded it, and basically it told me off for using hungarian notation.
 
Well I like using it - it makes things clearer!
 
The tool has potential, but any tool that tells me to stop putting m_ for internal class members can go jump Wink | ;-)
 
/**********************************
Paul Evans, Dorset, UK.
Personal Homepage "EnjoySoftware" @
http://www.enjoysoftware.co.uk/
**********************************/

QuestionCan I connect to a SQL Server database for the schema?memberJohn M. Drescher2 Sep '03 - 23:54 
I use access to create my database for MSDE directly. It would be nice to create a .sql file for that too. Also does it handle stored procesures and views?
 
John
GeneralNot Flashy, But...memberjdunlap1 Sep '03 - 10:27 
...meets a need. This is something I was looking for. Thanks!
 

"Blessed are the peacemakers, for they shall be called sons of God." - Jesus

"You must be the change you wish to see in the world." - Mahatma Gandhi






GeneralRe: Not Flashy, But...memberantidemon2 Sep '03 - 23:36 
There is a SQL Server upgrade wizard available from Microsoft which will also do this.

GeneralRe: Not Flashy, But...memberJohn M. Drescher2 Sep '03 - 23:50 
antidemon wrote:
There is a SQL Server upgrade wizard
 
Does it work with MSDE?? And is it available for download?
 

 
John
GeneralRe: Not Flashy, But...memberDamage_Inc3 Sep '03 - 1:41 
I've written something similiar that I used to convert my access database to mysql since I couldn't find a free one. Works pretty well except it can't handle binary data such as type BLOB. At some stage or another I'd like to incorporate more database types to convert to-and-from. But yeah....when I have time :P.
GeneralRe: Not Flashy, But...memberantidemon3 Sep '03 - 3:14 
I just succesfully ran a test using Office XP and MSDE 2.
 
From within your access database select Tools\Database Utilities\Upsizing Wizard - you may need to install this feature from the Office disks.
It's pretty straightforward, just follow the prompts.
 
I understand it's also available in Office 2000.
 
Take a look at this article:
 
http://support.microsoft.com/default.aspx?scid=kb;en-us;325017 - Office 2000
or
http://support.microsoft.com/default.aspx?scid=kb;en-us;330468 - Office XP
 

GeneralRe: Not Flashy, But...memberJohn M. Drescher3 Sep '03 - 3:59 
Thanks I'll try that..
 
John
GeneralRe: Not Flashy, But...memberDaniel Fisher (lennybacon)28 Oct '03 - 1:59 
year, but it sucks - or do you like the column "upsize_ts" and so on ...
 
# THIS CODE AND INFORMATION ARE PROVIDED
# "AS IS" WITHOUT WARRANTY OF ANY
# KIND, EITHER EXPRESSED OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE
# IMPLIED WARRANTIES OF MERCHANTABILITY
# AND/OR FITNESS FOR A PARTICULAR PURPOSE.
# http://www.lennybacon.com/
GeneralRe: Not Flashy, But...memberSteveC-A924 Oct '03 - 3:53 
Yeah, but that's not something you can wrap into a class or component and run as a "conversion factory". I do a lot of work with a place that has tons of MDBs and DBFs, and they need to be aggregated into MSSQL. This kind of tool will do the trick.
 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by Rowdy75
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid