Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:22:21+00:00 2026-06-06T23:22:21+00:00

i am a new linq to sql learner and this is my very first

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:22:22+00:00Added an answer on June 6, 2026 at 11:22 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im really new to linq-to-SQL so this may sound like a really dumb question,
I am new to LINQ to SQL and attempting to create a generic Data
I'm very new to using Linq-to-SQL, that's why I'm asking this question. I've searched
I'm new at LINQ, searching the net for LINQ samples that mimic SQL's LIKE
I'm new to Linq and SQL terminology - can someone tell me why this
I'm new to Linq to Sql, what I would like to achieve is to
How would you write this exact SQL query with the new Linq-to-NHibernate Provider (3.x)
I am new to LINQ to SQL, hence this question. By default if I
I am new to linq to sql I wrote this function: public ICollection<ICustomer> GetAll()
I'm new to LINQ to SQL and I would like to know how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.