Use Local Database File in C# Windows Application

Here I will explain how to use .SDF local database file in your window application. I will also explained 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

    Add- />New Item

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

    Add button

  3. Then go to->view menu->Server Explorer
    Here you can see the local database which is created by you.

    Server Explorer
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

    add column name and column data type

    In this Screen my table name and my column name.

    table name and my column name

Step 3: How to save Data in SDF database table?

I will use sqlserverce Name space.

Here I will insert , update, delete data from datatable which reflact 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.

    methods

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

    local database table

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

    Update method

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

    Delete method

This is my complete demo. Please download attachment for complete Demo. 


Similar Articles