Wednesday, May 18, 2011

Windows Application with Embedded Database

To have a windows desktop application with an embedded database. You can use MS SQL Compact edition.

To add the database to your application follow the below steps:

1) Goto solution explorer.
2) Right click on your application.
3) Goto add new item.
4) Add Local Database.
5) Right Click on Your application.
6) Add new reference to System.data.SqlServerCe.
7) Goto Server Explorer.
8) The newly Created databse wil be there, now you can add your tables.
9) To add an auto incriment column in the database, set identity of that feild to true, indentity incriment to 1, identity seed to 1.
10) The connection string would be

string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\cce.sdf";
connectionString = "datasource=" + dbfile;
11) Now all set to done.

Few limitations of using compact editions are:

1) It doesnt support Joins.
2) It dosent support .HasRows.
3) The biggest disadvantage is, when you are running the application directly through the VS, you wont be able to check the data in the databse, because when you run the project the database file is created on the temparory location and data will not goto into the datafile that has been created within the project.

No comments:

Post a Comment