65.9K
CodeProject is changing. Read more.
Home

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

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.72/5 (8 votes)

Aug 29, 2014

CPOL

1 min read

viewsIcon

84107

downloadIcon

43

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.