欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c#-WinForm-按照学号精确查询对象

程序员文章站 2022-05-28 23:26:22
...

学号精确查询示例

c#-WinForm-按照学号精确查询对象


后台查询方法

c#-WinForm-按照学号精确查询对象

/// <summary>
       /// 根据学号查询学员对象
       /// </summary>
       /// <param name="stuId"></param>
       /// <returns></returns>
        public StudentExt GetStudnetById(string stuId)
        {
            string sql = "Select StudentId,StudentName,Gender,Birthday,ClassName,StudentIdNo,";
            sql += "CardNo,PhoneNumber,StudentAddress,StuImage from Students ";
            sql += "inner join StudentClass on Students.ClassId=Students.ClassId ";
            sql += "where StudentId=" + stuId;
            SqlDataReader objReader = SQLHelper.GetReader(sql);
            StudentExt list = null;
            if(objReader.Read())
            {
                list = new StudentExt()
                {
                    StudentId = Convert.ToInt32(objReader["StudentId"]),
                    StudentName = objReader["StudentName"].ToString(),
                    Gender = objReader["Gender"].ToString(),
                    Birthday = Convert.ToDateTime(objReader["Birthday"]),
                    ClassName = objReader["ClassName"].ToString(),
                    StudentIdNo = Convert.ToDecimal(objReader["StudentIdNo"]),
                    CardNo = objReader["CardNo"].ToString(),
                    PhoneNumber = objReader["PhoneNumber"].ToString(),
                    StudentAddress = objReader["StudentAddress"].ToString(),
                    StuImage = objReader["StuImage"]is DBNull?"":objReader["StuImage"].ToString()

                };
            }
            objReader.Close();
            return list;

前台提交查询事件

c#-WinForm-按照学号精确查询对象

 //根据学号查询事件
        private void btnQueryById_Click(object sender, EventArgs e)
        {
            if(this.txtStudentId.Text.Trim().Length==0)
            {
                MessageBox.Show("请输入学号", "查询提示");
                this.txtStudentId.Focus();
                return;
            }
            //根据学号查询学员对象
            StudentExt objStudent = objStudentService.
                GetStudnetById(this.txtStudentId.Text.Trim());
            if (objStudent == null)
            {
                MessageBox.Show("您输入的学号不正确", "验证提示");
                this.txtStudentId.Focus();            
            }
            else
            {
                FrmStudentInfo objFrmStudentInfo = new FrmStudentInfo(objStudent);
                objFrmStudentInfo.Show();
            }

        }

跳出展示窗体

c#-WinForm-按照学号精确查询对象

c#-WinForm-按照学号精确查询对象

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;



namespace StudentManagePro
{
    public partial class FrmStudentInfo : Form
    {
        public FrmStudentInfo()
        {
            InitializeComponent();
        }
        public FrmStudentInfo(StudentExt objStu):this()
        {
            this.lblStuName.Text = objStu.StudentName;
            this.lblGender.Text = objStu.Gender;
            this.lblBirthday.Text = objStu.Birthday.ToShortDateString();
            this.lblClassName.Text = objStu.ClassName;
            this.lblStuIdNo.Text = objStu.StudentIdNo.ToString();
            this.lblCardNo.Text = objStu.CardNo;
            this.lblPhoneNumber.Text = objStu.PhoneNumber;
            this.lblStuAddress.Text = objStu.StudentAddress;
            this.pbStu.Image = objStu.StuImage.Length == 0 ? Image.FromFile("default.gif") : 
               (Image)new Common
               .SerializeObjectToString().DeserializeObject(objStu.StuImage);
        }

    }
}
相关标签: 对象