Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Connect to SQL Server using LINQ to SQL?

Sure this is a really dumb question, but how do I connect my C# console app to a SQL Server Instance?

Have done this plenty of times with c# Web Apps, but this is the first console app I've done.

The only data source I can see (in VS 2010) is SQL Server Database File - I can't see how to connect to a SQL instance on a different server.

like image 423
Ben Avatar asked Oct 26 '11 20:10

Ben


People also ask

Which LINQ API is used to connect and work with SQL Server?

LINQ to SQL is a component of . NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. Relational data appears as a collection of two-dimensional tables (relations or flat files), where common columns relate tables to each other.

Can I use LINQ with databases other than SQL Server?

LINQ to SQL is only for SQL Server and not for any other relational databases out there so we still need to wait for LINQ to SQL implementations for Oracle or IBM DB2.

Is LINQ to SQL still used?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.


3 Answers

Step 1: Add a connection to your Server Explorer

enter image description here

After that is added, you should see it appear under the server exlporer tab on the left side of your screen.

Step 2: Add a Linq to SQL file (.dbml) to your project

enter image description here

Step 3: Open the .dbml file from the file explorer on the right hand side of Visual Studio

Step 4: Open up your connection via the drop down arrows on you server browser so that you can see the tables you which to use in your console app

Step 5: Drag them onto the design area (as seen by the yellow lines and the result of doing this in the green box)

enter image description here

Step 6: Go back to your .cs page for your console, instantiate, and get to work

enter image description here

EDIT:

My guess would be that you may be missing SQL server from your computer. Check your start menu, do you have Microsoft SQL Server folder with a configuration tool in it as seen here?:

enter image description here

like image 104
KreepN Avatar answered Oct 16 '22 08:10

KreepN


When you've created your console application, in the Solution Explorer, choose Add New Item and you should get a dialog box something like this:

enter image description here

What you're looking for is the Linq-to-SQL Classes which creates a *.dbml model file.

For a very complete and thorough introduction to Linq-to-SQL, read Scott Guthrie's Using Linq to SQL blog series (many posts). Those things all apply to any kind of app using Linq-to-SQL to talk to SQL Server.

like image 29
marc_s Avatar answered Oct 16 '22 07:10

marc_s


It sounds like you want to create a new LINQ To SQL dbml to your console app, and use Visual Studio's Server Explorer as part of the process.

Add a .dbml to your project as per normal.

Your Server Explorer window in Visual Studio should allow you to create a new Connection. Ensure you're using SqlClient. Here you can enter the instance name of your SQL Server.

enter image description here

like image 1
p.campbell Avatar answered Oct 16 '22 09:10

p.campbell