Using SQL Server named parameters with ExecuteStoreQuery and ExecuteStoreCommand

后端 未结 2 1003
野趣味
野趣味 2021-01-11 16:44

I am trying to use SQL Server named parameters with ObjectContext.ExecuteStoreQuery and ObjectContext.ExecuteStoreCommand when calling a stored pro

相关标签:
2条回答
  • 2021-01-11 17:00

    FYI - do not name your stored procedures to start with sp_ as that's how System stored procs are named. The result is that the SQL server will look through all system stored procs first before finding your definition and will seem slower in production with many stored procs and calls of them.

    0 讨论(0)
  • 2021-01-11 17:11

    In order to get this to work as you would expect, then you need to set up the query text as a parameterized query. The tricky part is that you just need to make sure your parameters are named differently than the SP parameters:

    var cmdText = "[DoStuff] @Name = @name_param, @Age = @age_param";
    var @params = new[]{
       new SqlParameter("name_param", "Josh"),
       new SqlParameter("age_param", 45)
    };
    
    ObjectContext.ExecuteStoreQuery<MyObject>(cmdText, @params);
    
    0 讨论(0)
提交回复
热议问题