使用ASP。Net MVC和经典的ADO.Net

时间:2021-02-01 01:39:42

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.

我正在寻找一种使用Classic ADO访问存储过程的方法。Net,因为我是ASP新手。我不知道该怎么做。

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

大多数示例展示了使用ADO的CRUD操作。净实体框架。

4 个解决方案

#1


49  

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.

这样,控制器就不再依赖于数据访问层的实现:是否使用普通ADO。NET, NHibernate, EF,其他ORM,调用外部web服务,XML,你命名它。

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.

现在剩下的就是配置您最喜欢的DI框架,将适当的存储库实现注入到控制器中。如果明天您决定更改数据访问技术,没问题,那么只需编写IUsersRepository接口的不同实现,并重新配置DI框架以使用它。无需触摸控制器逻辑。

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.

您的MVC应用程序不再与数据存储方式绑定。这使得单元测试更容易被隔离,因为它们不再与特定的数据源紧密耦合。

#2


9  

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!

看看Dapper-dot-net -它是什么驱动这个网站-卓越,轻量级,基于纯粹的ADO。NET,非常好地支持存储过程——不能说太多关于它的好东西!

#3


2  

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.

ASP。NET MVC与您想要使用的任何数据库框架一起工作。您可以以任何您喜欢的方式检索数据(例如classic ADO.NET),并将结果数据模型传递给视图。您只需在视图中指定模型的类型,以匹配要传递给它的对象。

#4


1  

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).

如果你知道怎么用ADO。你可以在ASP中那样做。NET MVC(请注意,这是完全不同的框架之间没有相互依赖)。

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

您可以将DataAccess代码封装到Repostitories中,并在控制器中使用这些存储库;

#1


49  

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.

这样,控制器就不再依赖于数据访问层的实现:是否使用普通ADO。NET, NHibernate, EF,其他ORM,调用外部web服务,XML,你命名它。

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.

现在剩下的就是配置您最喜欢的DI框架,将适当的存储库实现注入到控制器中。如果明天您决定更改数据访问技术,没问题,那么只需编写IUsersRepository接口的不同实现,并重新配置DI框架以使用它。无需触摸控制器逻辑。

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.

您的MVC应用程序不再与数据存储方式绑定。这使得单元测试更容易被隔离,因为它们不再与特定的数据源紧密耦合。

#2


9  

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!

看看Dapper-dot-net -它是什么驱动这个网站-卓越,轻量级,基于纯粹的ADO。NET,非常好地支持存储过程——不能说太多关于它的好东西!

#3


2  

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.

ASP。NET MVC与您想要使用的任何数据库框架一起工作。您可以以任何您喜欢的方式检索数据(例如classic ADO.NET),并将结果数据模型传递给视图。您只需在视图中指定模型的类型,以匹配要传递给它的对象。

#4


1  

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).

如果你知道怎么用ADO。你可以在ASP中那样做。NET MVC(请注意,这是完全不同的框架之间没有相互依赖)。

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

您可以将DataAccess代码封装到Repostitories中,并在控制器中使用这些存储库;