---
title: "How to add/edit/retrieve data using Local Database file in Microsoft Visual Studio 2012?"  
description: "How to add/edit/retrieve data using Local Database file in Microsoft Visual Studio 2012?"  
author: "Steilla Mitchel"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159377/how-to-add-edit-retrieve-data-using-local-database-file-in-microsoft-visual-studio-2012  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How to add/edit/retrieve data using Local Database file in Microsoft Visual Studio 2012?

How to add/edit/[retrieve data](https://www.mindstick.com/forum/160347/how-do-you-retrieve-data-from-a-nosql-database-like-mongodb) using a [Local Database](https://www.mindstick.com/articles/1542/working-with-web-sql-database-html5) file in [Microsoft](https://www.mindstick.com/articles/12301/microsoft-is-trying-to-kill-passwords) [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2012?

## Replies

### Reply by Aryan Kumar

Sure, here are the steps on how to add/edit/[retrieve](https://www.mindstick.com/forum/34400/how-to-retrieve-form-values-in-controller-action) data using [local](https://www.mindstick.com/forum/546/how-to-create-local-user-in-windows-8) [database file](https://www.mindstick.com/interview/22849/at-a-same-time-can-multiple-instances-applications-access-a-single-database-file) in Microsoft [Visual](https://yourviews.mindstick.com/audio/1147/through-the-lens-a-visual-exploration-of-the-world) [Studio](https://answers.mindstick.com/qa/116181/google-launches-workspace-studio-to-create-automated-gemini-powered-agents) 2012:

1. Create a new Windows Forms project in Visual Studio 2012.
2. In the Data Sources window, add a new data source by right-clicking on the Data Sources node and selecting "Add New Data Source".
3. In the Choose a Data Source Type dialog box, select "Database" and then click "Next".
4. In the Choose a Database Connection dialog box, select "Microsoft SQL Server Database File" and then click "Next".
5. In the Choose a Database File dialog box, browse to the location of the local database file that you want to use and then click "Open".
6. In the Choose Your Data Connection dialog box, select the database that you want to use and then click "Finish".
7. Drag the tables from the Data Sources window onto your form.
8. Add code to your form to add, edit, and retrieve data from the database.

Here is an example of the code that you can use to add a new record to the database:

C#

```plaintext
private void buttonAdd_Click(object sender, EventArgs e)
{
  // Get the data from the form controls.
  string firstName = textBoxFirstName.Text;
  string lastName = textBoxLastName.Text;
  string email = textBoxEmail.Text;
  string phone = textBoxPhone.Text;

  // Add the record to the database.
  using (var connection = new SqlConnection("Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True"))
  {
    connection.Open();

    var command = new SqlCommand("INSERT INTO Customers (FirstName, LastName, Email, Phone) VALUES (@firstName, @lastName, @email, @phone)", connection);
    command.Parameters.AddWithValue("@firstName", firstName);
    command.Parameters.AddWithValue("@lastName", lastName);
    command.Parameters.AddWithValue("@email", email);
    command.Parameters.AddWithValue("@phone", phone);

    command.ExecuteNonQuery();
  }
}
```

This code will add a new record to the `Customers` table in the `MyDatabase` database. The `firstName`, `lastName`, `email`, and `phone` values are obtained from the form controls.

You can use similar code to edit and retrieve data from the database.


---

Original Source: https://www.mindstick.com/forum/159377/how-to-add-edit-retrieve-data-using-local-database-file-in-microsoft-visual-studio-2012

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
