标题:关于MVC调用存储过程
只看楼主
Sephirose
Rank: 1
来 自:四川-乐山
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-3-26
结帖率:55.56%
已结贴  问题点数:20 回复次数:2 
关于MVC调用存储过程
最近在学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,就是显示无法找到
搜索更多相关主题的帖子: public Movies create 
2013-09-10 11:01
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
得分:20 
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;  
       //-----------------------------------
       .................
   }
  .............
}


♂ 死后定当长眠,生前何须久睡。♀
2013-09-10 11:13
Sephirose
Rank: 1
来 自:四川-乐山
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-3-26
得分:0 
回复 2楼 303770957
就是想请教下该怎么用,MVC感觉和以前学的窗体应用不一样,没有可以被赋值的控件啊
2013-09-10 11:25



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-420378-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057994 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved