This article will help you to learn Database Connection in C#.
How to Connect a Database to our C# Window Forms?
At very first we need to know whether our IDE Software (Visual Studio) provides an in-built Database or not.
Visual Studio will provide you in-built SQL Database or you need to install Express Edition separately into your System.
You can download Microsoft SQL Server Management Studio from the Microsoft Official Website.
So let's start the SQL Database connection in C# Window Form.
Step 1. Very First we need to create a simple Window Form in which we take some data from the Users and store it in our Database.
![Simple Window]()
This image show a Simple Window Form which having 3 Labels, 3 TextBox, 4 Buttons, and a DataGridView Components which are in-built controls in C# Window Form.
If you look at the left side of the image, there are some common controls in Window Form C# which you can use easily in your Window Form, Simply using the Drag-and-Drop method.
Step 2. After creating a Form, We need to add a SQL Database in our Window Form.
So, left-click on Window Form's name which you can see in Solution Explorer. Then follow the way given below. ADD -->NEW ITEM..
![SQL Database]()
As given in the image, Simply you need to follow this and you will go to the next screen.
Step 3. In the third step, we need to search " Service-based Database " in the search box.
![Service-based]()
Then select the option as given in the image. And give a name to your database as you like in the Name Field with the ".mdf " extension. For example," MyDatabase.mdf ".
Step 4. Now you will see your database will show in the Server Explorer window on the left side of the screen.
![Server Explorer]()
If you are not able to see it, just refresh the Server Explorer window by clicking on the refresh button given in the top-left corner of the Server Explorer window.
Step 5. We have to give our table name as we want by just replacing the selected " TABLE" with our table name as given below in the image.
![TABLE]()
Step 6. Now simply add DataTable Feild according to your requirement in the Window Forms text field.
![Text field]()
You can set the length of the value of the Field as well as you can add constraints in your Database Field for example "PRIMARY KEY" , is a default constraint in the database.
Step 7. After Creating a Database table Field, Save the table by clicking on the " Update " Button.
![Update]()
Then you will see a Preview Database Update dialog box, Now just click on the "Update Database" Button to save your database table.
Now we have to refresh our Server Explorer Window. Your Database table will be added in your Window Form.
Step 8. To connect your Database to your Window Form, Simply we need to add a Database Connection String in our Programme code.
![Connection String]()
We will get the Database Connection String from the Properties Window of our database. And then Copy the Database Connection String that will paste at our Code.
Step 9. Now, we will paste the copied string into our Code Window.
![Code Window]()
As given in the image, we have to add one more thing which is a namespace " System.Data.SqlClient " in our Code.
And paste the copied string at " SqlConnection(@"/here/"); " in the code.
Example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Project_Name
{
public partial class Form1 : Form
{
SqlConnection con;
SqlCommand cmd;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection(@"");/paste your Database Connection String
con.Open();
}
}
}