actually connect to a SQL Server

SQLDude

Member
Joined
Jan 5, 2017
Messages
5
Location
Pittsburgh, PA
Programming Experience
10+
I've been trying to figure out how I can use LINQ to query a SQL Server database using C# for 2 days now. I've been through about a dozen tutorials but they all seem to lack that key step where you actually connect to a SQL Server database. I've been working in .NET and SQL Server for over 15 years now so it really shouldn't be this hard to figure out.
Now I feel like I'm losing my mind because I just read a tutorial where the guy sets up a database, then sets up his C# project with LINQ and then just starts dragging and dropping tables from the database into the application - somehow the application must have just guessed which database he wanted to use.
Would appreciate any help finding a tutorial that has ALL of the steps for joining C# to SQL Server using LINQ or even not using LINQ. I just need to be able to get database from the database so I can display it on the screen.
 
Firstly, I'd probably recommend that you use Entity Framework rather then LINQ to SQL. You use LINQ to Entities in much the same way as you use LINQ to SQL but EF supports databases other than SQL Server and also has far more features than L2S.

As for that tutorial, I think he made a small mistake in step 4.3. It actually says:
Now we should drag all the tables to the left hand side of the designer and save (as shown in the figure below).
but I think that it should say:
Now we should drag all the tables from the left hand side to the designer and save (as shown in the figure below).
I've never used L2S in that way but I believe that you choose which database you want to use in the Server Explorer or Database Explorer and when you drag tables from that window onto the L2S designer it will generate the appropriate connection string and use that at run time.

As for actually connecting to the database at run time, that is done implicitly. Whenever you make a call against your L2S context that requires data to be retrieved or saved, the context itself will open and close the connection as required.
 
I sort of stumbled across the solution which was that I needed to add the Server Explorer window to the project. I guess that is automatic in some cases but I had to find it on the view menu and add it to my project. Once I had that view open I could see what he was talking about in the tutorial. Can't imagine how many dozens of tutorials just assume that this window is displayed because there was no mention of it.
 
It's not a matter of adding it to your project. The Server Explorer is an IDE window, like the Solution Explorer, Toolbox or Properties, that is displayed or not, regardless of the project that is currently open. The Server Explorer has always been open by default for me whenever I've installed VS so I can only assume that you either closed it at some point, intentionally or not, or the initial configuration you selected when installing doesn't display it by default.
 
Back
Top Bottom