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
@fgeorgiew are you just needing to know how to populate a model (class) from a stored procedure? You could use an ORM like NHibernate or Entity Framework to handle the plumbing for you, or just use raw ADO.NET code, like in the example below. Note, this is just rough code, but you get the idea.
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;
}
}