Can I use pure SQL in ASP.net MVC?
hello guys i need to use pure SQL queries for my database project at the same time i wanna use ASP.net MVC so i can learn a new technol开发者_C百科ogy !!! can i use SQL with ASP.net MVC WITHOUT using " LINQ to SQL " ?! i'm still learning so i'm sorry if it so stupid using things the OLD SCHOOL but my instructor insists that he want to see a PURE SQL Queries
Yes. You can use whatever you want for your model layer. Note, you can use raw SQL queries with LINQ To SQL as well.
Northwnd db = new Northwnd(@"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
(@"SELECT c1.custid as CustomerID, c2.custName as ContactName
FROM customer1 as c1, customer2 as c2
WHERE c1.custid = c2.custid"
);
Or perhaps your instructor wants straight up ADO.NET, which works fine too. There is an example here.
You can use raw SQL queries in ASP.Net MVC the same way you use them anywhere else.
It can be helpful to use idioms like
using (var reader = command.ExecuteReader())
return View(reader.Select(dr => new { Name = dr["Name"], ... }));
EDIT: It appears that you're asking how to use ADO.Net in general.
Try this tutorial.
Of course you can use SQL in ASP.NET MVC. I'd consider keeping the SQL code on stored procedures rather than hard-coding the SQL directly in the application code. It's better to maintain and it's a good practice that will probably be looked upon by your instructor.
SQL has nothing to do with ASP.NET MVC. ASP.NET MVC is a web framework for building web sites and applications. My suggestion is that you try to abstract as much as possible, the MVC application shouldn't know if you're using SQL (stored procedures), Entity Framework, Linq-to-sql or if you get your data from a web-service.
精彩评论