skutik
skutik

Reputation: 111

How connect database created in SQL Server Management Studio with Visual Studio?

I've created a database Test1 in Microsoft SQL Server Management Studio and I would like to connect this database to my VS C# project. I found this question where are instructions how to connect just system databases (tables master, model, msdb and tempdb). Is there some way how to connect user-created table to my VS project?

Thank you for your answers and I hope that my problem is understandable.

enter image description here

Upvotes: 3

Views: 15799

Answers (1)

CuddleBunny
CuddleBunny

Reputation: 1981

Probably not the best way, but from Visual Studio:

  • Open the Server Explorer panel
  • Click "Connect to Database"

From here, there are two options depending on whether the database above was created for SQL Server or as a SQL Express file.

  • If the database came from a SQL Express .mdf file, select "Microsoft SQL Server Database File (SqlClient)" as the data source, and browse for the .mdf file.

OR

  • If the database resides in SQL Server, change the data source to "Microsoft SQL Server"
  • Enter localhost as the server name. This should enable the dropdown list under "Select or enter a database name."
  • Select Test1 from the drop down.

You should now see Test1 under data connections in the Server Explorer pane, finally:

  • Right click on it and select properties.
  • In the properties pane, you will see the Connection String which you can use to connect to the database with code in your project. From here, it depends on what type of project you have.

Upvotes: 6

Related Questions