2

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);
}
2
  • 2
    Have you added a reference to the System.Data.Linq assembly? Commented Jul 30, 2014 at 17:32
  • 1
    I added the reference and I can see it now, thanks Commented Jul 30, 2014 at 17:44

1 Answer 1

5

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.