asp.net-mvc – ASP.NET MVC:调用存储过程的最佳方式
我试图决定哪个是调用存储过程的最佳方法.
我是ASP.NET MVC的新手,我一直在阅读很多关于Linq的SQL和实体框架,以及Repository Pattern.说实话,我很难理解L2S和EF之间的真正差异,但我想确保我在应用程序中构建的是正确的. 对于现在,我需要正确地调用存储过程:a)保存一些用户信息并得到响应,b)为产品目录获取一些信息. 到目前为止,我已经创建了一个Linq to SQL .dbml文件,从Server Explorer中选择sotred过程,并将该实例拖到.dbml中.我现在正在调用存储过程: MyLinqModel _db = new MyLinqModel(); _db.MyStoredProcedure(args); 我知道有更多的参与…加上我在我的控制器里这样做,我明白这不是一个好的做法. 有人能认出我的问题在哪里吗? 解决方法
如果你想要做的就是调用存储过程,LINQ和EF可能是过度的.
我使用企业库,但ADO.NET也可以正常工作. 见this tutorial. 简短地(从参考文章中无耻地复制和粘贴): SqlConnection conn = null; SqlDataReader rdr = null; // typically obtained from user // input,but we take a short cut string custId = "FURIB"; Console.WriteLine("nCustomer Order History:n"); // create and open a connection object conn = new SqlConnection("Server=(local);DataBase=Northwind; Integrated Security=SSPI"); conn.Open(); // 1. create a command object identifying // the stored procedure SqlCommand cmd = new SqlCommand( "CustOrderHist",conn); // 2. set the command object so it knows // to execute a stored procedure cmd.CommandType = CommandType.StoredProcedure; // 3. add parameter to command,which // will be passed to the stored procedure cmd.Parameters.Add( new SqlParameter("@CustomerID",custId)); // execute the command rdr = cmd.ExecuteReader(); // iterate through results,printing each to console while (rdr.Read()) { Console.WriteLine( "Product: {0,-35} Total: {1,2}",rdr["ProductName"],rdr["Total"]); } } 更新 我错过了你说你在控制器中这样做的部分. 不,这不是正确的方法. 您的控制器应该真正只涉及编排视图构造.创建一个单独的类库,称为“数据访问层”或者较不通用的一些类,并创建一个类来处理调用存储过程,从结果中创建对象等.对于如何处理这些对象有很多意见,最常见的是: View | Controller | Business Logic | Data Access Layer |--- SQL (Stored procs) -Tables -Views -etc. |--- Alternate data sources -Web services -Text/XML files -blah blah blah. MSDN has a decent tutorial on the topic. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – 将返回文件的长时间运行进程
- 单声道是asp.net的可行替代方案吗?
- asp.net – asp文本框控件的CSS
- asp.net-mvc – UIHint属性在MVC中
- 哪个是在ASP.NET中使用字符串的最佳实践(C#)
- asp.net-mvc – Globalize.addCultureInfo不是一个函数
- Cannot access a disposed object in ASP.NET Core
- ASP.NET MVC Core中launchSettings.json修改导致VS2017无法
- asp.net-mvc – .Net会员提供商没有捕获电子邮件重复
- asp.net-mvc – mvc默认期限后的会话超时(20分钟)
- asp.net-mvc-2 – RedirectToRouteResult如何工作
- ASP.NET登录页面重定向问题
- asp.net-mvc – 在ASP.NET MVC视图中使用代码隐藏
- asp.net – asp:RadioButtonList’RepeatLayout
- .net – 存储加密密钥的位置
- asp.net-mvc – Layout.cshtml上的ASP.NET MVC搜
- ASP.Net Identity 2.0中的不同用户类型
- ASP.NET DataSource控件“没有命名容器”异常
- [ASP.NET MVC] 产生一维条码Barcode(Code 39、Co
- asp.net – 将单选按钮与相应的标签对齐