how to connect SQL Database in C# Windows

in this tutorial you will learn how to connect SQL Database in C# Windows with help of Visual Studio Using C# so guise let's get's started to create connection of SQL Database.

how to connect SQL Database in C# Windows

 

how to connect SQL Database in C# Windows

First Step:

You need to left click of mouse in  Solution Explolar which give left corner of Visual Studio. If you have not got this then you need to goes View Menu  and choose solution explolar. That’s fine.

solution exploler

Second Step:

You need to right click of mouse on Data Connection and choose Add Connection then you got a popup box.

Third Step:

Here we will copy or find Server Name from SQL Express Popup Box and paste in Visual Studio popup where given Server Name and choose your Database which given in image select or enter a database name and left click of mouse on Text Connection if you got Successfully message then Database Connected Successfully.

Sql Server (Sqlclient)

SQL Server Name

Select database from db

How to test connection success in visual studio

 

Fourth Step:

Now you need to find Connection string of database so you need to click on Advanced Button and copy all connection string text paste in your Sql connection string which given following code

Connection String

Connected Database in Solution Explorer

// namespace  
using System.Data;
using System.Data.SqlClient;

// connection of database
private void btnreg_Click(object sender, EventArgs e)
        {
            SqlConnection con =new SqlConnection("Data Source=WINCTRL-NAA1ARS\\SQLEXPRESS;Initial Catalog=dbdemo;Integrated Security=True");
            con.Open();
         
        }