【问题标题】:ASP .NET MVC 3 Models + stored proceduresASP .NET MVC 3 模型 + 存储过程
【发布时间】:2011-10-13 18:58:07
【问题描述】:

我是 ASP MVC 的新手,我不知道如何创建基于我的数据库中的存储过程的模型。我已经有与另一个应用程序一起使用的数据库,我的网页必须使用提到的数据库。

如果有人能向我展示一些描述如何做到这一点的正确方法的代码,我将不胜感激。 (如果我不清楚:我需要创建使用数据库中的存储过程的 ASP .NET 模型,仅此而已)

提前发送

【问题讨论】:

    标签: c# .net asp.net-mvc-3 stored-procedures models


    【解决方案1】:

    @fgeorgiew 您是否只需要知道如何从存储过程中填充模型(类)?您可以使用像 NHibernate 或实体框架这样的 ORM 来为您处理管道,或者只使用原始 ADO.NET 代码,如下例所示。请注意,这只是粗略的代码,但您明白了。

    public class MyModel
    {
        public int ModelId { get; set; }
        public string FirstName { get; set; }
    }
    
    public class SqlMyModelRespoitory : IMyModelRepository // optional for DI/IoC, assume interface with GetSingleModel method
    {
        public MyModel GetSingleModel()
        {
            MyModel model;
            string connString = "server=10.1.1.1;database=MyDb;uid=me;pwd=hidden";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "p_GetMyModelFromDb";
    
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            model = new MyModel 
                            {
                               ModelId = Convert.ToInt32(reader[0]),
                               FirstName = reader[1].ToString()
                            };
                        }
                    }
                }
            }
            return model;
        }
    }
    

    【讨论】:

    • 如果我想获取所有对象怎么办?
    【解决方案2】:

    您可以从一些表明您的意图的抽象开始:

    public interface IMyRepository
    {
        SomeModel Get(int id);
    }
    

    那么你可以编写一个使用你的存储过程的实现:

    public class MyRepositorySql: IMyRepository
    {
        public SomeModel Get(int id)
        {
            ... call your stored procedure
        }
    }
    

    然后设计您的控制器,使其将此抽象作为参数:

    public class MyController: Controller
    {
        private readonly IMyRepository _repository;
        public MyController(IMyRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index(int id)
        {
            var model = _repository.Get(id);
            return View(model);
        }
    }
    

    现在剩下的就是配置您的 DI 框架以将正确的实现传递给控制器​​的构造函数。正如您现在所看到的,控制器与获取数据的方式完全分离。无论您是在数据访问层中使用 StoredProcs、ORM 还是其他任何东西,都无关紧要。

    【讨论】:

    • 你让我落后 1 秒。
    • thx :) 我想我明白了。现在是实施它并查看结果的时候了
    【解决方案3】:

    如何在 Asp.Net MVC 中调用存储过程

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Data;
    using System.Data.Entity;
    using System.Net;
    using System.Data.SqlClient;
    
    public partial class StudentRecord
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Marks { get; set; }
        public string Grade { get; set; }
        public Nullable<System.DateTime> DOB { get; set; }
    }
    
    public class SqlMyModelRespoitory : First_Test_DBEntities 
    {
        public StudentRecord GetSingleModel()
        {
            StudentRecord model;
            string connString = @"Paste Your Local Connection String";
    
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
    
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    cmd.CommandText = "InsertStudent";
    
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            model = new StudentRecord
                            {
                                Id = Convert.ToInt32(reader[0]),
                                Name = reader[1].ToString(),
                                Marks = reader[1].ToString(),
                                Grade = reader[1].ToString(),
                                DOB = Convert.ToDateTime(reader[1])
                            };
                        }
                    }
                }
            }
            return model;
        }
    }
    

    【讨论】:

      【解决方案4】:

      好的,假设您有一个名为“客户”的 SP,它正在选择一些列,例如:

      身份证
      名称
      地址

      现在您将在模型文件夹中创建一个名为“Customer.cs”的模型类,并定义如下属性:

      public int ID { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
      

      这是你的模型课。

      【讨论】:

      • thx 4 回复,但我认为您没有正确理解我:) 我知道什么模型来自 RoR 或 Php 等,问题不在于将 C# 模型类映射到 Db 表,但问题在于如何使用我的数据库和 ADO .NET 中的存储过程来做到这一点。 (关于最后一个我不确定):)
      猜你喜欢
      • 2011-11-20
      • 1970-01-01
      • 2021-04-23
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多