Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Use Local DataBase file (.sdf) in Windows Application C#

0.00/5 (No votes)
29 Aug 2014 1  
How to use local database file (.sdf) in Windows application C#

Introduction

Here, I will explain how to use .SDF local database file in your Windows application. I will also explain how to save record and update record in local database. I will explain these points:

  • How to add Sdf file in application?
  • How to create table in SDF Database?
  • How to save data in SDF database table?

Step 1: How to Add Sdf File in Application?

  1. Go to->Solution explorer->right click on your application->Add->New Item.

  2. Go to->Data->Local dataBase->Click on Add button.

  3. Then go to->view menu->Server Explorer.

Here, you can see the local database which is created by you.

Step 2: How to Create Table in SDF Database?

  1. Then Go To->Server Explorer->Expand your database (MyDB.sdf) ->right click on->table->Create Table.

  2. Write your table name and add column name and column data type, length, etc. and click OK.

    In this screen, my table name and my column name.

Step 3: How to Save Data in SDF Database Table?

I will use sqlserverce namespace.

Here I will insert, update, delete data from datatable which reflect in actual database table.

  1. If you want to insert, update, delete data from local database, you need to add this namespace in your code System.Data.SqlServerCe.
  2. Then, write this method. In this method, I have to use sqlceConnection, SqlCeCommand, SqlCeCommandBuilder.

  3. Then write Add method. In this method, I have to write insert new record in local database table.

  4. Then write Update method. In this method, I have to write updation code.

  5. Then write Delete method. In this method, I have to write delete code.

This is my complete demo.

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