24

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.

KreepN
  • 8,528
  • 1
  • 40
  • 58
Ben
  • 4,281
  • 8
  • 62
  • 103

3 Answers3

40

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

KreepN
  • 8,528
  • 1
  • 40
  • 58
  • Cheers. It's step 1 that's causing a problem though. The only option I have under Data Source is: "Microsoft SQL Server Database File (SqlClient)" and the Change button is grayed out. Have I missed a step to enable "MS SQL Server (SqlClient)"? – Ben Oct 26 '11 at 20:54
  • Yep - got SQL Server 2008 tools installed. Weird thing is I also have Visual Studio Web Developer Express, and it works fine from here. – Ben Oct 26 '11 at 21:23
  • That is very odd. That would indicate that Visual Studio is the culprit. I searched around and didn't see anyone else with the exact same problem, so a quick reinstall, or (repair if it has that option) may help, but I'm never one to recommend that as the first step as its long and tedious. – KreepN Oct 26 '11 at 21:26
  • Yeah - I'm starting to think that's the best bet. :-( – Ben Oct 26 '11 at 21:27
  • Just checked and repair is in option under control panel, good luck :)! – KreepN Oct 26 '11 at 21:29
  • Step 6 DataClass1DataContext "The type or namespace name could not be found" After adding the dbml I compiled successfully and tried again but problem persists. – HMR Jul 31 '15 at 04:51
  • @KreepN: I done this steps but `mydataclassDataContex` object don't known in my console application project.why? – maryam Dec 24 '15 at 13:46
  • @sarina Might need to be more specific. Is the .Dbml file in the same project? If it is in a folder or some sub-directory, make sure you have a reference to that namespace in your code where you are trying to use the context. – KreepN Dec 27 '15 at 08:19
1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

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

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • The only option I have under Data Source is: "Microsoft SQL Server Database File (SqlClient)" and the Change button is grayed out. Have I missed a step to enable "MS SQL Server (SqlClient)"? – Ben Oct 26 '11 at 20:46