SQL Server 2005 Expressに接続してみるテスト

// Linq to SQL sample

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Northwind db = new Northwind(@"Server=.\SQLEXPRESS;Database=Northwind;Integrated Security=true");
            db.Log = Console.Out;

            #region 行をデータベースに挿入するには
            
            var insertOrderDetail = new OrderDetails
            {
                OrderID = 10248,
                ProductID = 51,
                UnitPrice = 42.40M,
                Quantity = 40,
                Discount = 0
            };
            // Add the new object to the Orders collection.
            db.OrderDetails.InsertOnSubmit(insertOrderDetail);

            // Submit the change to the database.
            try
            {
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
            #endregion            

            #region データベースの行を更新するには

            // Query the database for the row to be updated.
            var query = from ord in db.OrderDetails
                        where ord.OrderID == 10248
                        select ord;

            // Execute the query, and change the column values
            // you want to change.
            foreach (OrderDetails ord in query)
            {
                ord.Quantity = 50;
                // Insert any additional changes to column values.
            }

            // Submit the changes to the database.
            try
            {
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadLine();
            #endregion

            #region データベースから行を削除するには

            // Query the database for the rows to be deleted.
            var deleteOrderDetails = from details in db.OrderDetails
                                     where details.OrderID == 10248
                                     select details;

            foreach (var detail in deleteOrderDetails)
            {
                db.OrderDetails.DeleteOnSubmit(detail);
            }

            try
            {
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            } 
            #endregion
        }
    }
}
C:\>sqlmetal /server:.\SQLEXPRESS /database:Northwind /namespace:LinqSample /code:LinqSample.cs /language:csharp