NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Reading A Database Schema in SQLite with C#

Wednesday, September 16th, 2009

As an introduction to my next, still secret, blog post, I would like to go over how you can read the schema from a SQLite database, using C# and System.Data.SQLite.

This is a fairly simple process, but not immediately apparent if you are trying to do what I needed to do. In my case, I simply wanted to open an SQLite database (if you aren't familiar with SQLite, it is just a file-based DBMS), and read all of the tables within this database (file).

Without further ado, here is the code, which is fairly simple:

// Initialize the connection
SQLiteConnection conn = new SQLiteConnection("data source=" + file);

// These is how you list the schema of an SQLite database
SQLiteCommand comm = new SQLiteCommand("SELECT * FROM sqlite_master WHERE type = 'table'", conn);
conn.Open();
// Populate the reader
SQLiteDataReader reader = comm.ExecuteReader();

// Step through each row
while (reader.Read())
{
for (int a = 0; a < reader.FieldCount; a++)
{
// This will give you the name of the current row's column
string columnName = reader.GetName(a);

// This will give you the value of the current row's column
string columnValue = reader[a].ToString();
}
}

Pretty easy, but very useful.

SQLitin' Tom Out.

Tags

CSharp Databases SQLite

Related Blogs

Enumerating all attributes of an element and adding them to a dictionary using LINQ with Lambda Expressions
Writing console output from a Windows form in C#
Creating an Organizational Chart using C#: Part 1
Calculating ISO 8601 Date formats in C#, C++, and Java
Autostarting a Windows Service directly after install in C#

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations