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

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

namespace 第六章
{
    public partial class Form1 : Form
    {

        DBHelper db = new DBHelper();
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = null;
        public Form1()
        {
            InitializeComponent();
        }

  

        public void Sshow() 
        {

        }

        public void show() 
        {
            string sql = @"SELECT  [StudentNo] 
      ,[LoginPwd] 
      ,[StudentName] 
      ,[Sex] 
      ,[GradeId] 
      ,[Phone] 
      ,[Address] 
      ,[BornDate] 
      ,[Email] 
  FROM [MySchool].[dbo].[Student]";
            adapter = new SqlDataAdapter(sql,db.Connection);
            try
            {
                if (ds.Tables["student"] != null)
                    ds.Tables["student"].Clear();

                adapter.Fill(ds,"student");
                this.dataGridView1.DataSource = ds.Tables["student"];

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            show();
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //string s = this.treeView1.SelectedNode.Text;
            //int i = this.treeView1.SelectedNode.Level;
            //string message = string.Format("选中了{0}节点,深度是{1}",s,i);
            //MessageBox.Show(message,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            //if (this.treeView1.SelectedNode.Text.Equals("S1"))
            //{
            //    newView();
            //}
            //else
            //{
            //    show();
            //}  
            //int i = Convert.ToInt32(this.treeView1.SelectedNode.Parent.Tag);
            //string s = this.treeView1.SelectedNode.Text;
            SHITU();
        }

        public void newView()
        {
            //DataView dv = new DataView(ds.Tables["student"]);
            //dv.RowFilter = "gradeid =1";
            //this.dataGridView1.DataSource = dv;
        }


        public void SHITU() 
        {
            DataView dv = new DataView(ds.Tables["student"]);
           
            String sb = "";
            if (this.treeView1.SelectedNode.Level==2)
            {
                int i = Convert.ToInt32(this.treeView1.SelectedNode.Parent.Tag);
                string sex = this.treeView1.SelectedNode.Text;
                sb = "gradeid=" + i + " and sex='" + sex + "'";
               //sb = "gradeid=" + sex;
            }
            
            //if (this.treeView1.SelectedNode.Text.Equals("S1"))
            //{
            //    sb.Append("gradeid=1");
            //}
            //else if (this.treeView1.SelectedNode.Text.Equals("S2"))
            //{
            //    sb.Append("gradeid=2");
            //}
            //else if (this.treeView1.SelectedNode.Text.Equals("Y2"))
            //{
            //    sb.Append("gradeid=3");
            //}
                dv.RowFilter = sb;
                this.dataGridView1.DataSource = dv;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

           string s = this.dataGridView1.SelectedCells[0].Value.ToString();
           DialogResult choice= MessageBox.Show("确定要删除学号为"+s+"的学生信息吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Stop);
            if (choice==DialogResult.OK)
            {
                if (this.dataGridView1.SelectedRows.Count>0)
                {
                    s = this.dataGridView1.SelectedRows[0].Cells["studentNo"].Value.ToString(); 
                
                db.OpenConnection();
                try
                {
                    string sql = string.Format("DELETE FROM  Student  WHERE StudentNo='{0}'",s);
                    MessageBox.Show(sql);
                    SqlCommand comm = new SqlCommand(sql,db.Connection);
                    //comm.ExecuteNonQuery();
                    int i = comm.ExecuteNonQuery();
                    if (i==1)
                    {
                        MessageBox.Show("删除成功!!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                        show();
                        SHITU();
                    }
                   
                  
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            }

        }
   

  

       
    }
}

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