zoukankan      html  css  js  c++  java
  • EF 存储过程

    //create parameters to pass to the stored procedure  
    //First input Parameter
    var param1 = new SqlParameter { ParameterName = "@paramName1", 
    SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Input, Value = 1 }; 
    
    //Second input parameter
    var param2 = new SqlParameter { ParameterName = "@paramName2", 
    SqlDbType = SqlDbType.VarChar, Direction = ParameterDirection.Input, Value = "Test Input" };                 
    
    //third out parameter
    var param3 = new SqlParameter { ParameterName = "@paramName3", 
    SqlDbType = SqlDbType.VarChar, Direction = ParameterDirection.Output, Size = 255 }; 
    
    //compose the SQL
    var SQLString = "EXEC [dbo].[name of the Stored Proc] @paramName1, @paramName2, @paramName3"; 
    
    //Execute the stored procedure 
    var employees= DataContext.Employee.SqlQuery(SQLString, param1, param2, param3); 
    
    //or you can execute the SP using below 
    //var employees = DataContext.Database.SqlQuery<Employees>
    (SQLString, param1, param2, param3)

    Points of Interest

    1. In the above sample, DataContext would be the name of your DataContext Instance variable
    2. Employee would be name entity (POCO class) mapping to your database
    3. If the resultset returned by the SP would be mapped to the IEnumerable collection of Entity - The above sample would return IEnumerable<Employee>
    4. We can use <DataContext>.DataBase.SqlQuery method or <DataContext>.<EntityName>.SqlQuery
    5. Your out param declared would have value returned by SP
    6. The connect sting should have MARS = true - MultipleActiveResultSets=true
  • 相关阅读:
    python 全栈开发大纲
    2018/6/22 晚
    python——小知识
    变量与常量
    1.2数据结构-抽象数据类型的表示和实现
    1.1数据结构-基本概念和术语
    人机交互实践04-定位作业
    人机交互实践04-图像浮动至右边
    人机交互实践03-课堂作业2
    人机交互实践03-链接到的网页
  • 原文地址:https://www.cnblogs.com/zeroone/p/4604152.html
Copyright © 2011-2022 走看看