EF和LINQ 调用存储过程

好久没有更新文章了,最近项目比较忙都没什么时间来分享最近的问题。 今天遇到一个超级傻逼的问题。C#中调用存储过程,自己code也10来年了,这应该是很简单的问题了。今天有2个新的api,一个只有1个参数, 一个有10多个参数,先前没有注意到对象类型, 以为是EF的DbContext,结果后来才发现是LINQ的DataContext对象。以前调用存储过程都是靠设计界面封装成方法。 现在designer界面有500多张表, 几年没有维护了,大家要修改什么东东都是直接改代码。所以这里以后台代码调用存储过程为例。

EF调用存储过程

复制代码
  using (AdventureWorks2014Entities aw=new AdventureWorks2014Entities())
            {
                int ret = aw.Database.ExecuteSqlCommand("EXEC [HumanResources].[uspUpdateEmployeeLogin] @BusinessEntityID,@OrganizationNode,@LoginID,@JobTitle,@HireDate,@CurrentFlag ",
                    new SqlParameter("@BusinessEntityID",10),
                    new SqlParameter("OrganizationNode",DBNull.Value),
                    new SqlParameter("LoginID", @"adventure-works\michael6"),
                    new SqlParameter("JobTitle", "Research and Development Manager23"),
                    new SqlParameter("HireDate", DateTime.Now),
                    new SqlParameter("CurrentFlag",true)
                    );
            }
复制代码

注意这里的ExecuteSqlCommand第一个参数是字符串,这个字符串必须包含参数,如这里的@BusinessEntityID,其次这里的SqlParameter里面的参数名是可以包含@符号也可以不包含

LINQ调用存储过程

复制代码
  using (AWDataClassesDataContext aw=new AWDataClassesDataContext())
            {
                int BusinessEntityID=10;
                string OrganizationNode = null; //0x5AE178
                string LoginID=@"adventure-works\michael6";
                string JobTitle="Research and Development Manager12";
                DateTime HireDate=DateTime.Now;
                bool CurrentFlag=true;
                string sql = string.Format(" EXEC [HumanResources].[uspUpdateEmployeeLogin] @BusinessEntityID={0},@OrganizationNode={1},@LoginID=N'{2}',@JobTitle=N'{3}',@HireDate=N'{4}',@CurrentFlag=N'{5}'"
                    , BusinessEntityID.ToString(), "NULL", LoginID.ToString(), JobTitle.ToString(), HireDate.ToString(), CurrentFlag.ToString());
                var ret = aw.ExecuteCommand(sql);
                //var ret=aw.ExecuteCommand("EXEC [HumanResources].[uspUpdateEmployeeLogin] @BusinessEntityID={0},@OrganizationNode={1},@LoginID={2},@JobTitle={3},@HireDate={4},@CurrentFlag={5}"
                //    , BusinessEntityID, null, LoginID, JobTitle, HireDate, CurrentFlag); 
            } 
复制代码
ExecuteCommand方法的后面传值的参数不能是object对象,所以我这里只能拼接SQL字符串。 大家可能注意到这里调用存储过程与普通的sql语句没什么区别,只不过它的sql是调用存储过程如:
EXEC [HumanResources].[uspUpdateEmployeeLogin] @BusinessEntityID=10,@OrganizationNode=NULL,@LoginID=N'adventure-works\michael6',@JobTitle=N'Research and Development Manager',@HireDate=N'2015/6/29 22:30:15',@CurrentFlag=N'True'
这样的sql语句是比较危险的(可能有sql注入)。如果参数可能为Null,那么sql语句 还得动态拼接,比new SqlParameter("OrganizationNode",DBNull.Value)这种写法麻烦多了。我用反编译工具并没有看到DataContext里面的具体实现。
看来LINQ to SQL真的是该淘汰了。


 

posted on   dz45693  阅读(4224)  评论(2编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2013-06-29 transaction manager has disabled its support for remote/network transactions. 该伙伴事务管理器已经禁止了它对远程/网络事务的支持

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示