29

I am looking out for a way to access stored procedures using Classic ADO.Net, since I am new to ASP.Net MVC I do not know how to go about it.

Majority of the examples show CRUD operations using ADO.Net Entity framework.

Sandhurst
  • 1,180
  • 5
  • 26
  • 40
  • 1
    just linking these two articles, together. http://stackoverflow.com/questions/2243898/displaying-standard-datatables-in-mvc – yedevtxt Sep 06 '12 at 15:05

4 Answers4

52

You could have a repository:

public interface IUsersRepository
{
    public User GetUser(int id);
}

then implement it:

public class UsersRepository: IUsersRepository
{
    private readonly string _connectionString;
    public UsersRepository(string connectionString)
    {
        _connectionString = connectionString;
    }

    public User GetUser(int id)
    {
        // Here you are free to do whatever data access code you like
        // You can invoke direct SQL queries, stored procedures, whatever 

        using (var conn = new SqlConnection(_connectionString))
        using (var cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandText = "SELECT id, name FROM users WHERE id = @id";
            cmd.Parameters.AddWithValue("@id", id);
            using (var reader = cmd.ExecuteReader())
            {
                if (!reader.Read())
                {
                    return null;
                }
                return new User
                {
                    Id = reader.GetInt32(reader.GetOrdinal("id")),
                    Name = reader.GetString(reader.GetOrdinal("name")),
                }
            }
        }
    }
}

and then your controller could use this repository:

public class UsersController: Controller
{
    private readonly IUsersRepository _repository;
    public UsersController(IUsersRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index(int id)
    {
        var model = _repository.GetUser(id);
        return View(model);
    }
}

This way the controller is no longer depend on the implementation of your data access layer: whether you are using plain ADO.NET, NHibernate, EF, some other ORM, calling an external web service, XML, you name it.

Now all that's left is to configure your favorite DI framework to inject the proper implementation of the repository into the controller. If tomorrow you decide to change your data access technology, no problem, simply write a different implementation of the IUsersRepository interface and reconfigure your DI framework to use it. No need to touch your controller logic.

Your MVC application is no longer tied to the way data is stored. This makes it also easier to unit test your controllers in isolation as they are no longer tightly coupled to a particular data source.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 5
    Excellent approach - combine that with Dapper and you can save yourself another boatload of boring, mind-boggling left-right-assignment-code .... – marc_s Jul 14 '11 at 17:14
  • 1
    @marc_s, yes, from my experiments Dapper seems like a great light-weight ORM. I am already considering it for my next project. I am getting a little sick of NHibernate. – Darin Dimitrov Jul 14 '11 at 17:15
  • very very great explanation. I am really appreciate the way which combined Repository design pattern and pure Ado.net style. Thank you @Darin. – Frank Myat Thu Feb 08 '12 at 03:36
  • @Darin, If you don't mind, I would like to get suggestion to combine UNIT OF WORK pattern and which you now explain Repository + Classic ADO.net. – Frank Myat Thu Feb 08 '12 at 03:38
  • How can we call the Controller here?I mean in the Url like http://localhost:21781/Student?Id=4 which give error to me saying No parameter in the Controller Constructor. – Hemant Kumar Oct 26 '12 at 05:40
  • How to set the connectionString? I can't set parameters for my ninject binding: ninjectKernel.Bind().To(); – Patrik Apr 28 '13 at 16:11
  • 2
    Nice answer Darin! Expect performance benefits too with using good old ADO.Net - EF (in my experience), has significant drawbacks and is slow unless you take hours trying to understand the underlying SQL it emits and optomize accordingly. – Vidar Jul 31 '13 at 09:36
  • I use a similar approach, but have moved most of the repetive logic into its own class. So rather than injecting just the connection string into the constructor I inject my DAL class. – Louise Eggleton Dec 31 '14 at 13:28
  • 1
    "You could have a repository" - where? in which folder? – Paul Feb 19 '15 at 17:18
11

Check out Dapper-dot-net - it's what drives this site - excellent, light-weight, based on pure ADO.NET, supports stored procedures very nicely - can't say enough good things about it!

singhswat
  • 832
  • 7
  • 20
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
3

ASP.NET MVC works with any database framework you want to use. You can retrieve your data any way you prefer (such as classic ADO.NET) and pass the resulting data model to the view. You just have to specify the type of model in the View to match the object you are passing to it.

Daniel Young
  • 436
  • 1
  • 3
  • 6
2

If you know how to do it with ADO.NET you can do that in ASP.NET MVC (be aware, this are absolutely different frameworks have no dependency on each other).

You can encapsulate you DataAccess code into Repostitories and use those Repositories, in Controllers;

Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86