LINQ TO SQL:动态执行带有有参数的存储过程

这是一个小的细节,如果我们在LINQ TO SQL中动态执行存储过程,而且它带有参数的话,应该按照下面的方法进行

1. 存储过程

ALTER PROCEDURE dbo.GetCustomerByCountry
(@country NVARCHAR(50))
AS
    /* SET NOCOUNT ON */
    SELECT * FROM Customers WHERE Country=@Country

2. 代码

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            NorthwindDataContext db = new NorthwindDataContext();
            db.Log = Console.Out;


            var query = db.ExecuteQuery<Customers>("EXEC GetCustomerByCountry @country={0}", "USA");
            foreach (var item in query)
            {
                Console.WriteLine(item.CustomerID);
            }
        }


    }
}

3. 结果

image

posted @ 2010-01-03 11:53  陈希章  阅读(1089)  评论(0编辑  收藏  举报