ASP .NET MVC 3 Models + stored procedures

前端 未结 4 1432
无人及你
无人及你 2021-02-10 12:35

Im, new in ASP MVC and I don\'t know how to create Models which base on stored procedures from my db. I have already database which works with another application, and my web pa

4条回答
  •  面向向阳花
    2021-02-10 12:57

    How to call stored procedure in 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 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;
        }
    }
    

提交回复
热议问题