Rockford .Net User Group

The premier .Net User Group in the Northern Illinois Area
Welcome to Rockford .Net User Group Sign in | Join | Help
in Search

LINQ and SQL Server databases

Last post 10-25-2007, 11:09 AM by Ross Albertson. 0 replies.
Sort Posts: Previous Next
  •  10-25-2007, 11:09 AM 103

    LINQ and SQL Server databases

    This example is based on an example from Introducing Microsoft LINQ. I modified it to work with VS 2008 Beta 2.

    First, I created a Customer class that mapped to some of the fields in the database:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq.Mapping;

    namespace LinqToSql1
    {
        [System.Data.Linq.Mapping.Table(Name="Customers")]
        class Customer
        {
            [Column(Name="Customer ID")]
            public string CustomerID;
            [Column(Name="Company Name")]
            public string CompanyName;
            [Column]
            public string City;
            [Column(Name = "Region")]
            public string State;
            [Column]
            public string Country;
        }
    }

    Then I wrote the main part of the program, which looks for customers from the state of Washington.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Linq;

    namespace LinqToSql1
    {
        class Program
        {
            static void Main(string[] args)
            {
             DataContext db = new System.Data.Linq.DataContext("Northwind.sdf");
             Table<Customer> Customers = db.GetTable<Customer>();
             string SQLCommand = "DELETE FROM Customers WHERE City='Seattle'";

                var query =
                    from c in Customers
                    where c.Country == "USA" && c.State == "WA"
                    select new { c.CustomerID, c.CompanyName, c.City };

                foreach (var row in query)
                {
                    Console.WriteLine(row);
                }
            }
        }
    }
    Running this code produced the following:

    { CustomerID = LAZYK, CompanyName = Lazy K Kountry Store, City = Walla Walla }
    { CustomerID = TRAIH, CompanyName = Trail's Head Gourmet Provisioners, City = Ki
    rkland }
    { CustomerID = WHITC, CompanyName = White Clover Markets, City = Seattle }
    Press any key to continue . . .

    The SQL string is from an attempt to simulate a SQL insertion. When I placed it in my "where" clause, the "query" variable wasn't created and the foreach didn't execute. I even hard-coded that string in quotes and it ran safely. When I tried:

    where c.Country == "USA" && c.State == DELETE FROM Customers WHERE City='Seattle'

    it didn't even compile. LINQ doesn't recognize "DELETE", "INSERT", or "UPDATE" as commands, but rather as normal strings. Later, I might demonstrate how LINQ performs the equivalent of those SQL commands. One last thing: I used a version of the Northwind database as you might have guessed.

    Ross Albertson


    Lay a .NET over the world
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems