Jonathan Kittell
Jonathan Kittell

Reputation: 7493

Connect to Database with Connection String to use LINQ

I have a connection string and I want to use LINQ to query a remote database. In the Microsoft example they use the DataContext class. However the DataContext does not appear in Intellisense. It says that it uses 'System.Data.Linq` but I am not seeing that either. http://msdn.microsoft.com/en-us/library/bb350721(v=vs.110).aspx

Is there a Hello World example for using a connection string and LINQ?

public void SimpleQuery()
{
    var connectionString = @"Server=10.1.10.1;database=Mydatabase;uid=myusername;password=mypassword;";
    DataContext dc = new DataContext(connectionString);

    var q =
        from n in dc.table
        select n;

    Console.WriteLine(n);
}

Upvotes: 2

Views: 10067

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

Well, that is not how it works or at least it is not that simple.

In order to be able to run linq queries against your DB, first you need to map your db tables to dot net classes.

You can do that in various ways, for example you can use Linq to Sql, or Entity framework.

For EF, you need to decide which EF approach you are going to use (Model First,Code First etc.) Then you should configure your settings and create your db context.Take a look at Entity Framework documentation for more details...

Upvotes: 5

Related Questions