Create a new Windows Forms project in Visual Studio 2012.
In the Data Sources window, add a new data source by right-clicking on the Data Sources node and selecting "Add New Data Source".
In the Choose a Data Source Type dialog box, select "Database" and then click "Next".
In the Choose a Database Connection dialog box, select "Microsoft SQL Server Database File" and then click "Next".
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".
In the Choose Your Data Connection dialog box, select the database that you want to use and then click "Finish".
Drag the tables from the Data Sources window onto your form.
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#
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here are the steps on how to add/edit/retrieve data using local database file in Microsoft Visual Studio 2012:
Here is an example of the code that you can use to add a new record to the database:
C#
This code will add a new record to the
Customerstable in theMyDatabasedatabase. ThefirstName,lastName,email, andphonevalues are obtained from the form controls.You can use similar code to edit and retrieve data from the database.