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两个截取字符串的方法分享
- asp.net-mvc – Asp.Net Mvc – Html.TextBox – 设置自动对
- ASP.NET MVC – 返回JavaScriptResult不起作用
- asp.net-mvc-3 – MVC3比MVC2有什么优势?
- 一个新的ASP.NET MVC 5应用程序如何知道如何创建数据库,以及
- asp.net-mvc – 寻找第三方CMS与MVC网站集成
- asp.net-mvc – 添加视图模型类下拉列表不显示我的类
- 为什么在发送到asp.net web方法之前jquery不将我的数组转换
- ASP.NET和C#重定向
- asp.net – Internet Explorer中的图像加载超时
