关于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,就是显示无法找到