none
LINQ to SQL-Northwind Console application in VC# 2008 Express:4 Errors-DataContext and Table could not found, The name 'custs' does not exist in the current context RRS feed

  • Question

  • Hi all,

    I created a Console application in my Visual C# 2008 Express and copied the following code from a book:

    using System;
    
    
    
    using System.Collections.Generic;
    
    
    
    using System.Linq;
    
    
    
    using System.Data.Linq;
    
    
    
    using System.Data.Linq.Mapping;
    
    
    
    using System.Text;
    
    
    
    
    
    
    
    namespace scConsoleAppLinqToSql_Northwind
    
    
    
    {
    
    
    
        [Table]
    
    
    
        public class Customers
    
    
    
        {
    
    
    
            [Column]
    
    
    
            public string customerId;
    
    
    
            [Column]
    
    
    
            public string companyName;
    
    
    
            [Column]
    
    
    
            public string city;
    
    
    
            [Column]
    
    
    
            public string country;
    
    
    
        }
    
    
    
        class Program
    
    
    
        {
    
    
    
            static void Main(string[] args)
    
    
    
            {
    
    
    
                // connection string
    
    
    
                string connString = @"
    
    
    
                       server = NAB-WK-123456\SQLSCOTT;
    
    
    
                       integrated security = true;
    
    
    
                       database = northwind;
    
    
    
                       ";
    
    
    
    
    
    
    
                // create data context
    
    
    
                DataContext db = new DataContext(connString);
    
    
    
    
    
    
    
                // create typed table
    
    
    
                Table<Customers> customers = db.GetTable<Customers>();
    
    
    
    
    
    
    
                // query database
    
    
    
                var cuts =
    
    
    
                    from c in customers
    
    
    
                    select
    
    
    
                       c
    
    
    
                 ;
    
    
    
    
    
    
    
                // display customers
    
    
    
                foreach (var c in custs)
    
    
    
                    Console.WriteLine(
    
    
    
                        "{0}, {1}, {2}, {3}",
    
    
    
                        c.customerId,
    
    
    
                        c.companyName,
    
    
    
                        c.city,
    
    
    
                        c.country
    
    
    
                    );
    
    
    
            }
    
    
    
        }
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    

    I got the following 4 errors:

    Error 1 The type or namespace name 'DataContext' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scConsoleAppLinqToSql-Northwind\scConsoleAppLinqToSql-Northwind\Program.cs 34 13 scConsoleAppLinqToSql-Northwind
    Error 2 The type or namespace name 'DataContext' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scConsoleAppLinqToSql-Northwind\scConsoleAppLinqToSql-Northwind\Program.cs 34 34 scConsoleAppLinqToSql-Northwind
    Error 3 The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scConsoleAppLinqToSql-Northwind\scConsoleAppLinqToSql-Northwind\Program.cs 37 13 scConsoleAppLinqToSql-Northwind
    Error 4 The name 'custs' does not exist in the current context C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\scConsoleAppLinqToSql-Northwind\scConsoleAppLinqToSql-Northwind\Program.cs 47 31 scConsoleAppLinqToSql-Northwind

    Question #1:  Do I have to add an "Entity Class" to my project for getting "Data Context? If so, how can I add "Entity Class?
    Question #2:  What causes the 4 errors? How can I solve them?

    Please help and advise.

    Thanks,
    Scott Chang 


    SHC
    • Edited by Scott Chang Tuesday, August 25, 2009 6:17 PM
    Tuesday, August 25, 2009 6:15 PM

Answers

  • I found that "var cuts" was a mistake and I corrected it to "var custs". I also added Entity Class and Northwind.sdf to Database Explorer.  It worked nicely and I saw the right output in my Console application screen.  Here is my finalized code:

    // Adopted from pp 439-441 VVA & JH)
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Linq;
    using System.Data.Linq.Mapping;
    using System.Text;
    
    namespace scConsoleAppLinqToSql_Northwind
    {
        [Table]
        public class Customers
        {
            [Column]
            public string customerId;
            [Column]
            public string companyName;
            [Column]
            public string city;
            [Column]
            public string country;
        }
        class Program
        {
            static void Main(string[] args)
            {
                // connection string
                string connString = @"
                       server = NAB-WK-1234567\SQLSCOTT;
                       integrated security = true;
                       database = northwind;
                       ";
    
                // create data context
                DataContext db = new DataContext(connString);
    
                // create typed table
                Table<Customers> customers = db.GetTable<Customers>();
    
                // query database
                var custs =
                    from c in customers
                    select
                       c
                 ;
    
                // display customers
                foreach (var c in custs)
                    Console.WriteLine(
                        "{0}, {1}, {2}, {3}",
                        c.customerId,
                        c.companyName,
                        c.city,
                        c.country
                     );
                Console.ReadLine();
            }
        }
    }
    
    
      
    SHC
    • Marked as answer by Zhipeng Lee Monday, August 31, 2009 1:53 AM
    Wednesday, August 26, 2009 6:19 PM