如何将数据库表中的数据导入控制器而不使用实体框架到mvc 3中 [英] how to get data from database table into controller without using entity framwork into mvc 3

查看:59
本文介绍了如何将数据库表中的数据导入控制器而不使用实体框架到mvc 3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将数据库表中的数据导入控制器或模型而不使用实体框架进入mvc 3

how to get data from database table into controller or model without using entity framwork into mvc 3

推荐答案

您不想使用EF或不使用EF想使用任何ORM?



如果您不想使用任何ORM,那么您可以直接使用ADO.NET,但是您需要编写大量代码,例如将 DataTable 解析为对象列表,将对象属性解析为SP参数等。



例如,您将获得模型您的控制器(如果您提交表单然后模型绑定器将数据绑定到模型中)并将更改提交到数据库,您将编写参数SP。在此之后,对于SP的每个参数,您需要编写手动代码以将模型属性作为SP参数传递。



虽然您可以编写一些通用方法来避免代码冗余,但仍会有很多手动的东西。所以我个人认为你应该使用EF或任何其他ORM。
You don't want to use EF or don't want to use any ORM?

If you can don't want to use any ORM then you can use ADO.NET directly, but you would be required to write a lot of code, like for parsing DataTable to object list, parsing object properties to SP parameters etc.

For example you will get model into your controller (if you are submitting the form then model binder will bind the data into model) and to commit your changes to database you will have write the parameter SP. After that for each parameter of SP you would required to write manual code to pass the model properties as SP parameter.

Although you can write some generic methods to avoid code redundancy, but still there will be a lot of manual things. So in my personal opinion you should go with EF or any other ORM.


public IEnumerable<TrusteeEntity> GetTrustees(string urn)
        {
            IList<TrusteeEntity> trustees = new List<TrusteeEntity>();
            using (SqlConnection con = new SqlConnection(DatabaseHelper.MainConnectionString))
            {
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = "select tr.Id, tr.FirstName, tr.LastName, tr.Title, tr.Addr1, tr.Addr2, tr.Town, tr.County, tr.Postcode from Trt "+
                                      "inner join Trstees tr on t.Id;
                    con.Open();
                    var reader = cmd.ExecuteReader();
                    ReadT(reader, trustees);
                }
            }
            return trustees;
        }



您可以像我的示例那样使用ADO.NET方法。或者您可以使用与实体框架不同的ORM。或者您可以所有sql程序代替......有很多选项;)。


You can you ADO.NET approach like my example. Or you can use different ORM than Entity framework. Or you can all sql procedure instead... There is a lot of options ;).


这篇关于如何将数据库表中的数据导入控制器而不使用实体框架到mvc 3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆