注册 登录
编程论坛 ASP.NET技术论坛

关于MVC调用存储过程

Sephirose 发布于 2013-09-10 11:01, 3950 次点击
最近在学MVC,在MVC中调用存储过程
我的存储过程是
create Proc [dbo].[Movies_select]
as
set nocount on
begin
select * from Movies
end

在mvc中的MODELS下建了一个Movies类

public class Movies
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
        public string Raing { get; set; }
    }

    public class GetCustomerCName
    {
        private List<Movies> moviesList = new List<Movies>();

        public List<Movies> MoviesList
        {
            get
            {
                return moviesList;
            }
        }

        public GetCustomerCName(string connectionString)
        {
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("Movies_select", con);
             = CommandType.StoredProcedure;
            SqlDataReader dr = cmd.ExecuteReader();
            try
            {
                con.Open();
                if (dr.HasRows)
                {
                    dr.Read();
                    moviesList.Add(new Movies { Title = dr[1].ToString(), ReleaseDate = Convert.ToDateTime(dr[2].ToString()), Genre = dr[3].ToString(), Price = Convert.ToDecimal(dr[4].ToString()), Raing = dr[5].ToString() });
                }
                con.Close();
            }
            catch (Exception ex)
            {
                con.Close();
                throw new Exception(ex.Message);
            }
        }
    }

不过接下来应该如何在Controllers中读取moviesLis的值呢?试了几种方法,网上看的是
SqlRepository sr = new SqlRepository(WebConfigurationManager.ConnectionStrings["connectionString01"].ConnectionString);

不过却一直无法找到SqlRepository,编译器没显示是缺少USING,就是显示无法找到
2 回复
#2
3037709572013-09-10 11:13
List<Movies> lst=new GetCustomerCName(WebConfigurationManager.ConnectionStrings["connectionString01"].ConnectionString).MoviesList;
if(lst!=null&&lst.Count>0)
{
   Movies movies=new Movies();
   for(int i=0;i<lst.Count;i++)
  {
        movies = lst[i] as Movies ;//获取
       // ---------看你怎么用了--------------------
         //movies.ID;
        // movies.ReleaseDate;      
        // movies.Genre;      
         //movies.Price;        
         //movies.Raing;  
       //-----------------------------------
       .................
   }
  .............
}

#3
Sephirose2013-09-10 11:25
回复 2楼 303770957
就是想请教下该怎么用,MVC感觉和以前学的窗体应用不一样,没有可以被赋值的控件啊
1