I am new to Asp.net MVC and I am also new to SQL server. I am able to write a long query with joins etc to retrieve the required data in MS SQL server. Now I want to display the query result in my MVC project as a new page. I dont want to use Linq because it will be another Big job to transform that sql query in Linq. I searched getdatafromquery() method but couldnt find any good examples. I am not even sure what I need to do to implement it. Can any one please let me know what will be the best way to achieve it. Thanks.
2 Answers
I suggest you to use Entity Framework Code-First by following the steps mentioned on Getting Started with Entity Framework 6 Database First using MVC 5
On the other hand, regarding to "retrieving data from sql server using query in ASP.net MVC" you might have a look at my answer on Execute raw SQL query in ASP.NET MVC, database first mode
3 Comments
drmaa
Thanks a lot. I am working on an existing project which is already using entity framework etc. The second part (your answer) is what I am looking for. I am trying to see if i can follow it.
Murat Yıldız
You are welcome. But for the next projects, I strongly recommend to try EF Code First as it makes very easy performing the data field changes.
Murat Yıldız
@Asbat If the answer is helpful for you, please do not forget to mark it as answer. Thanks...
So I achieved this by performing the following steps.
- Create the sql-server query that you want to execute from C#.
- In a controller create an ActionResult method for writing the query and connectionstring to the database.
- Create a model that contains the same number of properties that is returned by the query
- Create a view.