动态存储过程名称linq to sql

时间:2011-03-31 12:11:38

标签: linq-to-sql

有没有办法将存储过程名称作为字符串传递给函数然后使用反射来实际获取sp在linq to sql查询中使用?

2 个答案:

答案 0 :(得分:4)

试试这个

var sp = typeof(DataContext).GetMethod("GetUsersByID"); //Get the SP
var result = sp.Invoke(DbContext, new object[]{100}); //Execute the SP with 100 as the parameter

答案 1 :(得分:3)

        AdventureWorksDataContext ad = new AdventureWorksDataContext();
        var sp1 = typeof(AdventureWorksDataContext).GetMethod("uspGetManagerEmployees");//Get the SP 
        var result1 = sp1.Invoke(ad, new object[] { 16 });

        var sp = typeof(AdventureWorksDataContext).GetMethod("GetEmployee");//Get the SP 
        var result = sp.Invoke(ad, new object[] {  });//If no parameters are passed