November 2013 Entries

Using C# to interface with SQLite

If you need to interface with SQLite there are a couple of steps. 1) You need to get the .net provider for SQLite from sourceforge.net 2) Then add a reference to System.Data.SQLite to your project. 3) You need to make sure the reference is marked to be copied locally. Here is a C# class to for doing select, insert, update, and delete. public class SqlLiteHelper { String dbConnection; ...

How to get a list of all identity columns in a database

Here is some code to get a list of identity columns in database. select so.name as TableName, + o.list as IdentityColumnName from sysobjects so cross apply (SELECT column_name + case when exists ( select id from syscolumns where object_name(id)=so.name and name=column_name ...

Check if a database exists on a server

Here is some code to use to check if a database exists. Just replace the string 'DATABASE NAME’. SELECT * FROM [master].[sys].[databases] WHERE name='DATABASE NAME'

How to switch Entity Framework database connected to

If you have a connection to a database through entity framework and you need to switch it to another database (with the exact same structure) you just need to set the Connection.ConnectionString (as seen below). I had an application where we created a copy of the Master database when setting up a new client.  So using Entity Framework I switched from the master database to the client (depending on what the admin was doing). using (MasterEntities aEntities = new MasterEntities()) { //Switch db connected to aEntities.Database.Connection.ConnectionString = aEntities.Database.Connection.ConnectionString.Replace("OldDatabaseName", "NewDatabaseName"); ...