写一个查询表中记录的存储过程,并在winform中应用他实现显示student表中的所有记录(app.config,引用,ClassModel类,List泛型绑定,dataGradView1的数据源)。
form1中的代码:
string constr = ConfigurationManager.ConnectionStrings["SchoolconStr"].ConnectionString;
private void Form1_Load(object sender, EventArgs e)
{
List<ClassModel> list = new List<ClassModel>();
using (SqlConnection conn = new SqlConnection(constr))
{
conn.Open();
string sp_name = "usp_class_selectAll";
using (SqlCommand cmd =new SqlCommand(sp_name,conn))
{
cmd.CommandType = CommandType.StoredProcedure;
using(SqlDataReader reader=cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
ClassModel model = new ClassModel();
model.clsName = reader.IsDBNull(reader.GetOrdinal("FclsName")) ? string.Empty : reader.GetString(reader.GetOrdinal("FclsName"));
model.clsDesc = reader.IsDBNull(reader.GetOrdinal("FclsDiecription")) ? string.Empty : reader.GetString(reader.GetOrdinal("FclsDiecription"));
list.Add(model);
}
}
}
}
}
dataGridView1.DataSource = list;
}
存储过程:
create procedure usp_class_selectAll
as
begin
select * from T_Class
end
exec usp_class_selectAll
效果: