开发者

Using stored procedure im MVC3 razor

开发者 https://www.devze.com 2023-03-27 21:14 出处:网络
I\'m started to learn mvc3 razor. I know, that this question is simple, but I really don\'t know how I can use stored procedure (mssql) in mvc3 razor.开发者_JAVA百科 Can someone show me a sample, how

I'm started to learn mvc3 razor. I know, that this question is simple, but I really don't know how I can use stored procedure (mssql) in mvc3 razor.开发者_JAVA百科 Can someone show me a sample, how I can use it?


Your choice of data access implementation has nothing to do with the tools you use to generate your User Interface and consequently, nothing to do with ASP.NET MVC 3 and Razor.

Read up on Entity Framework which is a good choice for data access in .NET applications and can be used to execute your stored procedure.


MVC3 Razor is a Web Framework. It is designed for displaying stuff on web pages. MVC has nothing whatsoever to do with data access.

ADO.NET is responsible for that. There are a number of frameworks built on top of ADO.NET (if you don't want to use the raw thing) to help you, such as LINQ to SQL, Entity Framework (both from Microsoft) or NHibernate (open-source).

If you look at one of those you may have more luck getting data from a stored procedure.


It says above to "make sure you answer the question".....none of the above really do that.....just tell you to look something up.

So....

a) Write your stored procedures to handle CRUD actions ie Get single record, get list of records, save record (insert and update) and delete record and maybe special algorithm procedures

b) Write a data access classes that have methods that use these stored procedures and return a data structure (class) of the required type derived from the SQL result set, You will have to design these of course.....these provide your model (M) in the MVC pattern. So your controller will reference your data access namespace or class, and your view will use the Razor @model to refer to the relevant data structure.

The key thing is to generate classes that can be used as a model....how you generate those classes does not matter (EF, NHibernate, stored procedures)....or at least it is your decision. Once you have these classes you can use them in Controller and View.

Most examples describing MVC use EF to generate the classes. But I always say that you have to get the data from a database regardless and you should understand what SQL is being used by EF to get/save the records.

0

精彩评论

暂无评论...
验证码 换一张
取 消