Click here to Skip to main content
15,886,075 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
1.72/5 (13 votes)
29 Aug 2014CPOL1 min read 82.3K   42   9   7
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.

    Image 1

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

    Image 2

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

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

Image 3

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.

    Image 4

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

    Image 5

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

    Image 6

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.

    Image 7

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

    Image 8

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

    Image 9

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

    Image 10

This is my complete demo.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer MNC
India India
I am C# corner MVP and Blogger and currently working as a .Net Developer in an MNC Company India and have one and eight month experience in C#,ASP.NET,SQL server.I am a B.Tech in computer science.This is My Blog where i blog https://aspdotnetmyblog.com/
This is a Collaborative Group (No members)


Comments and Discussions

 
QuestionWhere is the code for FillDataInDataset? Pin
Cesar8018-Mar-16 4:13
Cesar8018-Mar-16 4:13 
QuestionData is not saved when values entered programmatically Pin
Anish Joy18-Jul-15 21:46
Anish Joy18-Jul-15 21:46 
AnswerRe: Data is not saved when values entered programmatically Pin
pritik8894-Oct-15 20:38
pritik8894-Oct-15 20:38 
GeneralMy vote of 2 Pin
Klaus Luedenscheidt29-Aug-14 17:06
Klaus Luedenscheidt29-Aug-14 17:06 
Questionsample project Pin
fredatcodeproject29-Aug-14 12:25
professionalfredatcodeproject29-Aug-14 12:25 
SuggestionCode as text Pin
Wendelius29-Aug-14 4:28
mentorWendelius29-Aug-14 4:28 
SuggestionSQLite Pin
adriancs29-Aug-14 4:05
mvaadriancs29-Aug-14 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.