Programmingempire
In this post on How to Setup a Connection with SQL Server Database in Visual Studio, I will demonstrate a step by step procedure for creating an SQL Server Database and set up a connection. Also, I will describe how to retrieve data using a C# application.
Let us start with a console application in C# and all the steps are given below. Specifically, I will use Visual Studio 2019 for creating this application. However, the same procedure is also applicable with earlier versions of Visual Studio.
Set up a Connection with SQL Server Database in Visual Studio
- Start Visual Studio and Create a New Project
- Now, select a Console App and provide a suitable name.
- Once, the project opens, select Server Explorer from the View menu.
- Further, click on the server explorer and then right-click on Data Connection and select Add Connection.
- With this, you will see a dialog box for choosing the database server. Therefore, you can select SQL Server and name the database. It will ask you for creating a new database. Hence, select yes.
- Now, again click on server explorer and right-click on the newly created database, and then select Add Table.
- Now, specify a suitable name for the table and add columns. Finally, click on Update to update the database.
- Now, refresh the database in server explorer. It will show you the newly created table.
- Right-click on the table name and select Show Table Data.
- Now you can enter the data in the table. Once done, save the table and close it.
- For connecting this database with our C# application, right-click on the database name and select Properties. Copy the value of the ConnectionString property that we will use later in the program.
Example of Creating a Database Application in Visual Studio 2019
![Create a New Project](https://ci6.googleusercontent.com/proxy/J_aBngR47j49RwXV8pjRrZmYaBD9WTNy-L1SQ-qnAASXo5qv5WOBHxs2-Txz-mAEfmBY3mbCvo7RTppRfdGOSoCbmFgpf3dkJtJH90MEP0wJAKs-ri7e4ur91srDrpch2g=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-60-1024x717.png)
Establish the Data Connection
![Right-Click on Data Connection](https://ci3.googleusercontent.com/proxy/n4OwNNUJV08suqPdPvLdXhEOkzrzYoI_HPR7lwUU8B7JVMS6pSJgGchTLAyFxznLsm3fVzGb5NFW7cviOqwU1TrX86GzO3vLEP78Dd1bjxXnXVlvJyscoQ=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-61.png)
![Select the Add Connection Option](https://ci6.googleusercontent.com/proxy/V4lNY30NRmPiy1xLMmIR_2E0oea_oNShGJcrj6hd0wlnL-KRp1RfFhBnS8O5nBL792sIpsnibTkYuhWtXV8c-5NCz0_JC9sHOrEbtiDnxH1KqFxgr4L7UQ=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-62.png)
Ensure that the Data Source is Microsoft SQL Server Database File (SqlClient) and enter a new database file name.
![Specify Data Source and Database File Name](https://ci4.googleusercontent.com/proxy/etEHJ72kZ6tkytmqUlr7sLpIk3CdKZ-F8atxRsW8L78XKcmObo5QyCK4BAApgoID3yp6IWT9PwAKaceH-EWH3CMN6BqJI9Fw5Z7oFrVWDWHZ8c03gYSApQ=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-63.png)
You will get following Dialog Box. Click on Yes and view the newly created database in the Server Explorer.
![Create New Database File](https://ci5.googleusercontent.com/proxy/BDAoPptVpelMP26C7q604PK4CgWwUS8Gf8qRJXJV8MMPN7tP714CJVUZmExvJ5nAmQuCb5S9m5G1yp7OgU--5WO3D7W9oRYJjXm57tpKgzTYneKMl0YOZA=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-64.png)
![Database Created](https://ci3.googleusercontent.com/proxy/jt_Gjr4XspyW1z9vG-g_SzNLzeCQatuFAwzrPGV4skp4Eb6X_L7yZ6H4CyLv5rtuHZfZDuH_d32vYTOwI-PF25BQtGCSnTVGokOhOcGeLP324nkV9wOFvw=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-65.png)
Now expand the MyDb.mdf and right-click on the Tables tab as shown below. Also, select the Add New Table option.
![Add a Table to the Database](https://ci6.googleusercontent.com/proxy/4qLPhjM-q-6f0057-qJaetJexr5XW0TKX96-Pdr7IiX8jX_u3Rjqy8YlJ5kUMyZ52sEl1pvAq3U6RthtQoRBuqDrM4v7wXT7HxPpgSTeGf9J4XJluaHlbw=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-66.png)
Next, change the table name and add columns in the table and save the table. Finally, click on Update as shown below.
![Create a Table in the Database](https://ci5.googleusercontent.com/proxy/oLc3xerUCq8_R7p-C1MAKE9nomVKbzaFkCKGmuHf3tGIqA7n_I4r2OuW54xSPk0oeI9eh_vv2AyJ1yj0_onyeqGspFW0rTW6cCdnNK3q69K88GHfbEpCUSE4G20tec_A7w=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-68-1024x593.png)
Click on the Update Database Button.
![Click on Update Database](https://ci3.googleusercontent.com/proxy/7aXAPtKaXpygeNQRsApe-agTALkVTDHCUX74iZoUZmYW21tXQAiy8fnvUfCG4qeK8HhAWdDI6xXYfDkf8GXiHlVt5En1n1x7J9nVpbn-rwZ5-aIH7iYqOw=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-69.png)
Now refresh the Tables tab in the Server Explorer and right-click on the table Student. Further, select the option Show Table Data.
![Show Table Data](https://ci5.googleusercontent.com/proxy/SdIJbljn9jpWH49d6KSwV8lx2kJ6diGP7WXDX4-YUMEuJqNSCk4nSFGbjGtBIdtruAR0C33ZkPoJlZlroIP7D0VyQ1VIEorynNNSW_e83oeJgBwLFbRLgQ=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-70.png)
Now, enter some data in the table, save the table, and close it.
![Enter Data in the Student Table](https://ci4.googleusercontent.com/proxy/hjWc24QBcD1tmScjhkeGTJDpWv2MTMuex91YAVB2beSRnsml4DlRJh-bFVLuHXIwXqLxKltHPbC8z7FssLzYF2gYpHkSLDErC8EO8T_KFlAi8lugwPVpFg=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-71.png)
Now, right click on the database again in Server Explorer and select the Properties option.
![View Properties of the Database](https://ci3.googleusercontent.com/proxy/Gx3pBzCWD7sy01YnBih_LjahJczTlEfcrUZn0O_NAJ_OXwvtEqOGLf-El4K-w48_FmQ4-e0tbmC5PkMnKTf-aIb0LAo5igOWSS-kiyQGWyKX-iJCSGeRAQ=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-72.png)
Search the ConnectionString property, copy its value and keep it for later use in the application.
![Connection String Property](https://ci6.googleusercontent.com/proxy/y2WZtMm_IZ_ILEVkR3TZcgiIRrcIwNfow9TIQxmrhcgO3iE8EelZtGvx5XRajX-lrpEA8KlcTrmUPPkIObHKezRL16s71kdJuwzh4_wwrRS55ZAwRAUC6g=s0-d-e1-ft#https://programmingempire.com/wp-content/uploads/2020/12/image-73.png)
Now, database connection is complete and we can write the code to access it.
Summary
In this article on How to Setup a Connection with SQL Server Database in Visual Studio, you have learned creating a SQL Server database and connecting it with our application for the purpose of accessing data. Now, we can perform more operations like insert, update, and delete using the same database.
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
![programmingempire](https://www.programmingempire.com/wp-content/uploads/2021/10/pefavicon.png)