3

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

Community
  • 1
  • 1
skutik
  • 111
  • 1
  • 2
  • 8
  • Please elaborate on "connect". What _exactly_ do you want to accomplish? See [MSDN: Querying the Database](https://msdn.microsoft.com/en-us/library/bb882680%28v=vs.110%29.aspx), [Proper way to make a connection and query to a SQL Server Express database](http://stackoverflow.com/questions/9348148/proper-way-to-make-a-connection-and-query-to-a-sql-server-express-database), [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx). – CodeCaster Jun 09 '15 at 12:51
  • I'm sorry, this is my first question and I'm little confused of interface of this site, but it's my fault. I should read the question rules befor I ask a question... I want to connect my created table from MS SQL Studio to my VS project, where I would like to take data from database and use it in my program. – skutik Jun 09 '15 at 13:03

1 Answers1

6

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.
CuddleBunny
  • 1,941
  • 2
  • 23
  • 45