Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Linq to Sql tutorial codes and SQL Server Express 2008 : Expand / Collapse
Posted Sunday, November 11, 2012 8:46 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 13, 2012 5:09 PM
Points: 4, Visits: 7
Linq to Sql tutorial codes and SQL Server Express 2008 :

I am testing some tutorial sample codes for Linq to SQL using Visual Studio 2010 on 64 bits laptop, SQL Server Express (32 bits, x86) 2008.
OS is Windows 7.

Here is the code sample I tried:
a.1) Walkthrough: Simple Object Model and Query (C#)
Code here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace LinqConsoleApp
// Mapping a Class to a Database Table:
[Table(Name = "Customers")] // Mapping a Class, Customer, to a Database Table, Customers.
public class Customer
private string _CustomerID;
[Column(IsPrimaryKey = true, Storage = "_CustomerID")]
public string CustomerID
get { return this._CustomerID; }
set { this._CustomerID = value; }

private string _City;
[Column(Storage = "_City")]
public string City
get { return this._City; }
set { this._City = value; }


class Program
static void Main(string[] args)
//Specifying the Connection to the Northwind Database
DataContext db = new DataContext( @"C:\linqtest5\NorthwindAndPubsSample\NORTHWND.MDF");

Table<Customer> CustomerTable = db.GetTable<Customer>();

db.Log = Console.Out;// Attach the log to show generated SQL.
// Create a query command ( deferred action / execution )
// Query for customers in London.
IQueryable<Customer> custQuery =
from cust in CustomerTable //Customers
where cust.City == "London"
select cust;
//Executing the Query
foreach (Customer cust1 in custQuery)
Console.WriteLine("ID={0}, City={1}", cust1.CustomerID, cust1.City);

// Prevent console window from closing.

(Problem described later )
I am not able to post screenshots..... so here is my description
What has been done:My problems is connection with SQL Server 2008 Express in Win 7.
I have installed several versions of SQL instances ( SQL Server Express (x86) 2008, (x86) 2012, and 64-bits 2012 ) and using Visual Studio.

Then I have set up the following states:
SQL Server Express (x86) 2008, R2--automatic startup,
SQL Server Express (x86) (x86) 2012 -- manual startup,
SQL Server Express 64-bits 2012 -Manualo Start up.

Then the following exists:

1) Only instance SQL Server Express (x86) 2008 (R2) is running using Services.msc screenshot, as well as displaying registered servers using SQL Server Management Studio's view>Registered Servers and then clicking the Local Server Group tree node.

2) I was able to access run SQL Server Management Studio and able to connect to the database displaying some table content.
In this case I am using NorthWind sample data for tutorial.

3) Sql Server Configuration Manager 's SQL Server Services showing SQL server SQLEX2008X86NAME is running. The other two instances are not running.

4) Sql Server Configuration Manager 's SQL Server Network Configuration showing Protocols for SQLEX2008X86NAME with shared memory and Named Pipes enabled. All tutorial ran on a single laptop.

When I ran the C# “Linq to SQL” code sample above, it ran, and hangs here
on this line #49 of code:
foreach (Customer cust1 in custQuery)

After time-out, a SQL exception shows up with this message below:

“A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)”

The exception details is like so: ( I skip posting this....... for now )

I do have 3 SQL servers installed, but only one is running.
In addition, I ran services.msc, and it shows only SQLEX2008X86NAME is running

I followed the example code, and I also showed only ONE server is running, and I was able to access the database ( NorthWind) using SQL Server management Studio Express and display the table named “Customers”.
There is no means of specifying which server to run.
How do I specify which server to run?

The SQL exception does not tell me what server my application sample is trying to connect with!
When we have 3 servers installed, how do Linq to SQL knows which to connect to?

What do I need to do to make the code samples work?

Do I uninstall all SQL servers, and start over, and just install one only?
There are many components per installed server version.
How do I uninstall each version cleanly?

It should not be that hard to test tutorial codes...... this is unreal....

Thanks for your responses.
Post #1383442
Posted Monday, November 12, 2012 9:50 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, November 14, 2016 3:41 AM
Points: 257, Visits: 208
Hi There,

Run visual Studio and open the Server Explorer window.
Select Add Connection
Select Microsoft SQL Server Client
Select the running Server instance from the drop down box. Click Refresh if necessary. If you see no instance VS cannot see or connect to SQL Server.

If you see any databases in the Select or enter a database name drop down, you definitely have a working server instance.

Assuming that you see the database you want, you can go to your code and instantiate a SQLConnection object and use that instead of the path to the database file in your DataContext constructor.

That should at least give you a fighting chance.

I am no LinqToSQL expert as I use Entity Framework to generate my contexts but I hope this might help.

Post #1383752
Posted Tuesday, November 13, 2012 5:12 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 13, 2012 5:09 PM
Points: 4, Visits: 7


OK.... After much reading, it seems people are moving to Entity Framework.
Where can I get a good tutorial on EF using C#, VS2010, and a compatible database sample ( if applicable).

Post #1384347
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse