使用C#开发数据库应用系统----------------- 第三章 上机练习

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.LargeIcon;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.SmallIcon;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Details;
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ListViewItem li = new ListViewItem("新",5); 
            li.SubItems.Add("本地磁盘");
            li.SubItems.Add("19.3GB ");
            li.SubItems.Add("0.65GB");
            this.listView1.Items.Add(li);
      
            
        ListViewItem lii= new ListViewItem("新W", 7);
        lii.SubItems.AddRange(new string[]{"本地磁盘","40.96GB","4.6GB"});
        listView1.Items.Add(lii);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            tian();
        }

        private void tian() {
            if (listView1.Items.Count > 0) 
            {
                listView1.Items.Clear();
            }
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SELECT S.[StudentNo],S.[StudentName],S.[Sex],G.[GradeName]");
            sql.AppendLine(" from Student as S,Grade AS G");
            sql.AppendLine("where S.[GradeId]= G.[GradeId]");
            sql.AppendFormat("AND S.[StudentName] LIKE '%{0}%'",this.textBox1.Text.Trim());
            MySchool.DBHelper d = new MySchool.DBHelper();
            try
            {
                SqlCommand command = new SqlCommand(sql.ToString(), d.Connection);
                d.OpenConnection();
                SqlDataReader reader = command.ExecuteReader();
                if (!reader.HasRows)
                {
                    MessageBox.Show("没有要查找的纪录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                }
                else
                {
                    while (reader.Read())
                    {
                        string studentNo = reader["StudentNo"].ToString();
                        string studentName = reader["StudentName"].ToString();

                        string Sex = reader["Sex"].ToString();
                        string gradeName = reader["GradeName"].ToString();

                        //int genderId = Convert.ToInt32(reader["gender"]);
                        //string gender;
                        //if (genderId == (int)Gender.Male)
                        //{
                        //    gender = "男";
                        //}
                        //else
                        //{
                        //    gender = "女";
                        //}
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(studentName);
                        item.SubItems.Add(Convert.ToString(Sex));
                        item.SubItems.Add(gradeName);

                        listView1.Items.Add(item);


                    }
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            finally {
                d.CloseConnection();
            
            
            }

        
        }
    }       
}

你可能感兴趣的:(使用C#开发数据库应用系统----------------- 第三章 上机练习)