2

i am a new linq to sql learner and this is my very first attempt to create a data viewer program. The idea is simple, i'd like to create a software that is able to view content of a table in a database. That's it.

I got an early problem here already and i have seen many tutes and articles online but I still cant fix the bug.

Here is my code:

    static void Main(string[] args)
    {
        string cs = "Data Source=localhost;Initial Catalog=somedb;Integrated Security=SSPI;";

        var db = new DataClasses1DataContext(cs);
        db.Connection.Open();

        foreach (var b in db.Mapping.GetTables())
            Console.WriteLine(b.TableName);

        Console.ReadKey(true);
    }

When I tried to check db.connection.equals(null); it returns false, so i thought i have connected successfully to the database since there is no error at all. But the code above doesn't print anything out to the screen.

I kind of lost and don't know what's going on here. Does anyone know what is going wrong here?

2
  • How many mappings are there in the data context? Try Console.WriteLine(db.Mapping.GetTables().Count();? A mapping is not the same as a table. Commented Jul 2, 2012 at 7:07
  • Exactly! There is no mapping at all in the data context, but the count() return a number related to the mappings count that i have after dragging some mappings into the designer. Thanks! Commented Jul 3, 2012 at 23:34

2 Answers 2

3

Ok, let's look at some of these lines:


var db = new DataClasses1DataContext(cs);

This is a perfectly normal and fine call to a constructor. Since DataContext implements IDisposable, when you are using it for real, consider using the using statement.

using (var db = new DataClasses1DataContext(cs))
{
  // do stuff with db here
} // when leaving the block, db is disposed - even in the case of an exception.

db.Connection.Open();

Don't do this. DataContext will open and close the connection when it needs to.


foreach (var b in db.Mapping.GetTables())
  Console.WriteLine(b.TableName); 

Hmm, maybe there are no tables in the mapping. Did you drag a table onto the designer surface from the server explorer?

Most people would query a table instead of perusing the mappings. Consider this code instead:

foreach (var customer in db.Customer.Take(10))
{
  Console.WriteLine(customer.Name); 
}

Here's a video showing how to drag a table onto the designer surface from the server explorer:

http://www.youtube.com/watch?v=z9L11qrw9gk

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

1 Comment

All the things you said are true! I didn't add any table in the mapping so gettables.count() returned 0. Big thanks!
1

Try change your connection string to below

string cs = @"Data Source=.\;Initial Catalog=somedb;Integrated Security=SSPI;";

1 Comment

Thanks for replying my question but no luck after trying it out tho. I did some other combinations of the connection string too, still can't get any data out of it. Seems like i am missing some kind of property to make linq talk to sql server which i can't figure out yet.

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.