Sql Server中写查询表存储过程,并在winfrom中的dataGridView控件中显示表内容

本文介绍了一个使用Sql Server存储过程查询班级表(Class)的方法,并展示了如何通过C#代码调用该存储过程来获取数据并填充到List集合及DataGridView控件中。

用到的表:Class


Sql Server 存储过程:

create procedure usp_Class_select

as
begin
  select * from Class

end

---------------------------------------------------------------------------------------------------------------

app.configer:

<connectionStrings>
 <add name="MySchoolconStr" connectionString="Data Source=PC_THINK-THINK;Initial Catalog=MySchool;User ID=sa; Password=111111"/>
  </connectionStrings>

---------------------------------------------------------------------------------------------------------------

ClassModel2.cs类:

 public class ClassModel2
    {
        public string clsName
        {
            get;
            set;
        }
        public string clsDesc
        {
            get;
            set;
        }
    }

---------------------------------------------------------------------------------------------------------------


后台代码:

 string Constr = ConfigurationManager.ConnectionStrings["MySchoolconStr"].ConnectionString;
        
        private void 查询表中记录的存储过程_Load(object sender, EventArgs e)
        {
            List<ClassModel2> list = new List<ClassModel2>();
            using (SqlConnection conn = new SqlConnection(Constr))
            {
                conn.Open();
                string sp_name = "usp_Class_select";
                using (SqlCommand cmd = new SqlCommand(sp_name, conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader reader =cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ClassModel2 model = new ClassModel2();
                                model.clsName=reader.IsDBNull(reader.GetOrdinal("cName"))?string.Empty:reader.GetString(reader.GetOrdinal("cName"));
                                model.clsDesc = reader.IsDBNull(reader.GetOrdinal("cDescription")) ? string.Empty : reader.GetString(reader.GetOrdinal("cDescription"));
                                list.Add(model);
                            }
                        }
                    }
                }
                dataGridView1.DataSource = list;
            }
        }
        

运行结果:


好的,要实现WinForm学籍管理系统+SQL Server数据库实现多查询显示学生姓名、学号、身份证信息、班级、所属学院、专业名称,具体实现过程如下: 1. 数据库设计:需要设计学生、班级、学院、专业等,通过主键和外键进行之间的关联。 2. 数据库连接:在WinForm应用程序中,需要连接SQL Server数据库,可以使用System.Data.SqlClient命名空间中的类进行连接。 3. 多查询:在WinForm应用程序中,可以使用SQL语句进行多查询,例如: ``` SELECT student.name, student.sid, student.idcard, class.classname, college.collegename, major.majorname FROM student JOIN class ON student.classid = class.id JOIN major ON class.majorid = major.id JOIN college ON major.collegeid = college.id ``` 4. 数据显示:在WinForm应用程序中,可以使用DataGridView控件显示查询结果。可以将查询结果绑定到DataGridView控件上,然后设置DataGridView列的HeaderText属性为学生姓名、学号、身份证信息、班级、所属学院、专业名称,设置DataGridView数据源为查询结果。 下面是一个示例代码: ```csharp using System; using System.Data; using System.Data.SqlClient; using System.Windows.Forms; namespace StudentManagementSystem { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnQuery_Click(object sender, EventArgs e) { string connectionString = "Data Source=(local);Initial Catalog=StudentDB;Integrated Security=True"; string sql = "SELECT student.name, student.sid, student.idcard, class.classname, college.collegename, major.majorname " + "FROM student " + "JOIN class ON student.classid = class.id " + "JOIN major ON class.majorid = major.id " + "JOIN college ON major.collegeid = college.id"; SqlDataAdapter adapter = new SqlDataAdapter(sql, connectionString); DataTable table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table; dataGridView1.Columns[0].HeaderText = "学生姓名"; dataGridView1.Columns[1].HeaderText = "学号"; dataGridView1.Columns[2].HeaderText = "身份证信息"; dataGridView1.Columns[3].HeaderText = "班级"; dataGridView1.Columns[4].HeaderText = "所属学院"; dataGridView1.Columns[5].HeaderText = "专业名称"; } } } ``` 在上面的代码中,btnQuery_Click事件处理程序中,首先定义了连接字符串和SQL语句,然后创建了SqlDataAdapter对象,使用Fill方法填充查询结果到DataTable中,最后将DataTable设置为DataGridView数据源,设置列的HeaderText属性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值