ASP.NET MVC:调用存储过程的最佳方式 [英] ASP.NET MVC: Best Way To Call Stored Procedure
问题描述
我正在尝试确定调用存储过程的最佳方式.
I'm trying to decide which is the best way to call a stored procedure.
我是 ASP.NET MVC 的新手,我阅读了很多关于 Linq to SQL 和实体框架以及存储库模式的文章.老实说,我很难理解 L2S 和 EF 之间的真正区别……但我想确保我在应用程序中构建的内容是正确的.
I'm new to ASP.NET MVC and I've been reading a lot about Linq to SQL and Entity Framework, as well as the Repository Pattern. To be honest, I'm having a hard time understanding the real differences between L2S and EF... but I want to make sure that what I'm building within my application is right.
现在,我需要正确调用存储过程来:a) 保存一些用户信息并获得响应,b) 获取产品目录的一些信息.
For right now, I need to properly call stored procedures to: a) save some user information and get a response and, b) grab some inforation for a catalog of products.
到目前为止,我已经创建了一个 Linq to SQL .dbml 文件,从服务器资源管理器中选择了 sotred 过程并将该实例拖到 .dbml 中.我目前正在像这样调用存储过程:
So far, I've created a Linq to SQL .dbml file, selected the sotred procedure from the Server Explorer and dragged that instance into the .dbml. I'm currently calling the Stored Procedure like so:
MyLinqModel _db = new MyLinqModel();
_db.MyStoredProcedure(args);
我知道必须有更多的参与......而且我是在我的控制器中这样做的,我认为这不是一个好的做法.
I know there's got to be more involved... plus I'm doing this within my controller, which I understand to be not a good practice.
有人能认出我的问题在哪里吗?
Can someone recognize what my issues are here?
推荐答案
如果您只想调用存储过程,那么 LINQ 和 EF 可能有点矫枉过正.
LINQ and EF are probably overkill if all you're trying to do is call a stored proc.
我使用企业库,但 ADO.NET 也可以正常工作.
I use Enterprise Library, but ADO.NET will also work fine.
请参阅本教程.
简要(从引用的文章中无耻地复制粘贴):
Briefly (shamelessly copied-and-pasted from the referenced article):
SqlConnection conn = null;
SqlDataReader rdr = null;
// typically obtained from user
// input, but we take a short cut
string custId = "FURIB";
Console.WriteLine("
Customer Order History:
");
// 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"]);
}
}
更新
我错过了您说您在控制器中执行此操作的部分.
I missed the part where you said that you were doing this in your controller.
不,这不是正确的做法.
No, that's not the right way to do this.
你的控制器应该只参与编排视图构建.创建一个单独的类库,称为数据访问层"或不那么通用的东西,并创建一个处理调用存储过程、从结果创建对象等的类. 关于如何处理这个问题有很多意见,但也许是最常见的是:
Your controller should really only be involved with orchestrating view construction. Create a separate class library, called "Data Access Layer" or something less generic, and create a class that handles calling your stored procs, creating objects from the results, etc. There are many opinions on how this should be handled, but perhaps the most common is:
View
|
Controller
|
Business Logic
|
Data Access Layer
|--- SQL (Stored procs)
-Tables
-Views
-etc.
|--- Alternate data sources
-Web services
-Text/XML files
-blah blah blah.
这篇关于ASP.NET MVC:调用存储过程的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!