学生成绩查询系统

1,登录界面的设计

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.SqlClient;
 11 using System.Reflection;
 12 
 13 
 14 namespace WindowsFormsApplication2
 15 {
 16     public partial class Form0 : Form
 17     {
 18         public Form0()
 19         {
 20             InitializeComponent();
 21         }
 22 
 23         //SqlConnection conn = new SqlConnection("server=.;database=wisystem;integrated security=true;");
 24 
 25         /*窗体间的传值*/
 26         public static string username = string.Empty; 
 27 
 28         private void button1_Click(object sender, EventArgs e)      //login
 29         {
 30             if (ID.Text == "" || Passwd.Text == "" || ID.TextLength < 5 )
 31             {
 32                 MessageBox.Show("请输入完整的信息", "信息提示",
 33                 System.Windows.Forms.MessageBoxButtons.OK,
 34                 System.Windows.Forms.MessageBoxIcon.Warning);
 35                 ID.Text = "输入账号";
 36                 Passwd.Text = "请输入密码";
 37             }
 38             else if (ID.Text != "" && Passwd.Text != "")
 39             {
 40                 string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 41                 SqlConnection con = new SqlConnection(connection);
 42                 con.Open(); 
 43                 string strsql = "select ID,Passwd from user$  where ID='" + ID.Text.ToString().Trim() 
 44                     + "' and Passwd='" + Passwd.Text.ToString().Trim() + "'";
 45                 DataSet ds = new DataSet();
 46                 SqlDataAdapter da = new SqlDataAdapter(strsql, con);
 47                 da.Fill(ds);
 48                 string c = this.Identify.SelectedIndex.ToString ();
 49 
 50                 if (ds.Tables[0].Rows.Count > 0 && (ID.Text.ToString() == "admin") && Convert.ToInt32(Identify.SelectedIndex.ToString()) == 0)
 51                 {
 52 
 53                     MessageBox.Show("登录成功", "提示");
 54                     Form1 form1 = new Form1();
 55                     form1.Show();
 56                     this.Hide();
 57                 }
 58                 else
 59                     if(ds.Tables[0].Rows.Count > 0 && (ID.Text.ToString() == "admin") && Convert.ToInt32(Identify.SelectedIndex.ToString()) == 1)
 60                 {
 61                     MessageBox.Show("身份验证错误,已更改身份", "提示信息", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning);
 62                     Identify.SelectedIndex = 0;
 63                 }
 64                     else
 65                         if (ds.Tables[0].Rows.Count > 0 && (Passwd.Text.ToString() == "123456") && Convert.ToInt32(Identify.SelectedIndex.ToString()) == 1)
 66                         {
 67                             MessageBox.Show("登录成功", "提示");
 68                             username = ID.Text;
 69                             Form4 form4 = new Form4();
 70                             form4.Show();
 71                             this.Hide();
 72                         }
 73                         else
 74                             if (ds.Tables[0].Rows.Count > 0 && (Passwd.Text.ToString() == "123456") && Convert.ToInt32(Identify.SelectedIndex.ToString()) == 0)
 75                             {
 76                                 MessageBox.Show("身份验证错误,已更改身份", "提示信息",System .Windows .Forms .MessageBoxButtons .OK,  System.Windows.Forms.MessageBoxIcon.Warning);                               
 77                                 Identify.SelectedIndex = 1;
 78                             }
 79                             else
 80                             {
 81                                 MessageBox.Show("用户名或密码输入错误!");
 82                             }
 83                 con.Close();              
 84             }
 85         }
 86 
 87         private void Form0_Load(object sender, EventArgs e)
 88         {
 89             Identify.SelectedIndex = 1;
 90             ID.Text = "输入账号";
 91             Passwd.Text = "请输入密码";
 92             timer1.Enabled = true;
 93         }
 94 
 95         private void ID_MouseClick(object sender, MouseEventArgs e)
 96         {
 97             ID.Text = "";
 98             Passwd.Text = "";
 99         }
100 
101         private void Passwd_KeyDown(object sender, KeyEventArgs e)
102         {
103             Passwd.PasswordChar = '*';
104         }
105 
106         private void timer1_Tick(object sender, EventArgs e)
107         {       
108             this.label4.Left -= 2;
109             if (label4.Right < 0)
110             {
111                 this.label4.Left = 900;
112             }
113             
114         }
115 
116         private void timer2_Tick(object sender, EventArgs e)
117         {
118             this.textBox1.Text = DateTime.Now.ToString();
119         }
120 
121     }
122 }
View Code

2,学生查询界面

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data.SqlClient;
 11 using System.Reflection;
 12 
 13 namespace WindowsFormsApplication2
 14 {
 15     public partial class Form4 : Form
 16     {
 17         public Form4()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22          
 23 
 24         private void ShowData_Click(object sender, EventArgs e)
 25         {
 26             this.dataGridView1.Columns[0].HeaderText = "学号";
 27             this.dataGridView1.Columns[1].HeaderText = "姓名";
 28 
 29             Form0 form0 = new Form0();
 30             DataSet ds = new DataSet();
 31             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 32             SqlConnection con = new SqlConnection(connection);
 33             string sql = "select * from score$ ";
 34             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 35             try
 36             {
 37                 da.Fill(ds);
 38                 if (ds.Tables[0].Rows.Count > 0)
 39                 {
 40                     dataGridView1.DataSource = ds.Tables[0];
 41                 }
 42             }
 43             catch (Exception ex)
 44             {
 45                 throw new Exception(ex.ToString());
 46             }
 47             finally
 48             {
 49                 con.Close(); //close database;
 50                 con.Dispose();// free connect
 51                 da.Dispose(); //
 52             } 
 53         }
 54 
 55         private void Form4_Load(object sender, EventArgs e)
 56         {
 57             timer1.Enabled = true;
 58             this.Location = new Point(100,150);
 59             //MessageBox.Show(Form0.username);
 60             DataSet ds = new DataSet();
 61             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 62             SqlConnection con = new SqlConnection(connection);
 63             string sql = "select * from score$ where ID='"+Form0 .username +"'";
 64             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 65             try
 66             {
 67                 da.Fill(ds);
 68                 if (ds.Tables[0].Rows.Count > 0)
 69                 {
 70                     dataGridView1.DataSource = ds.Tables[0];
 71                 }
 72             }
 73             catch (Exception ex)
 74             {
 75                 throw new Exception(ex.ToString());
 76             }
 77             finally
 78             {
 79                 con.Close(); //close database;
 80                 con.Dispose();// free connect
 81                 da.Dispose(); //
 82             }
 83             this.dataGridView1.Columns[0].DataGridView.ColumnHeadersHeight = 80;
 84             this.dataGridView1.Columns[0].Width = 90;
 85             int i = 1;    
 86             for (i = 1; i < 14; i++)
 87             {
 88                 this.dataGridView1.Columns[i].Width = 60;
 89             }
 90             this.dataGridView1.Columns[0].HeaderText = "学号";
 91             this.dataGridView1.Columns[1].HeaderText = "姓名";
 92             this.dataGridView1.Columns[7].HeaderText = "计算机";
 93             this.dataGridView1.Columns[8].HeaderText = "C语言";
 94             this.dataGridView1.Columns[13].HeaderText = "名次";
 95         }
 96 
 97         private void timer1_Tick(object sender, EventArgs e)
 98         {
 99             this.textBox1.Text = DateTime.Now.ToString ();
100         }
101     }
102 }
View Code

3,管理员登录成功后的主界面

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Reflection;
 11 using System.Data.SqlClient;
 12 
 13 namespace WindowsFormsApplication2
 14 {
 15     public partial class Form1 : Form
 16     {
 17         public Form1()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 23 
 24         private void ShowData_Click(object sender, EventArgs e)
 25         {
 26             form1Refresh();
 27         }
 28 
 29         //刷新方法
 30         public void form1Refresh()
 31         {
 32             dataGridView1.DataSource = null;
 33             DataSet ds = new DataSet();
 34             SqlConnection con = new SqlConnection(connection);
 35             string sql = "select * from student";
 36             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 37             try
 38             {
 39                 da.Fill(ds);
 40                 if (ds.Tables[0].Rows.Count > 1)
 41                 {
 42                     dataGridView1.DataSource = ds.Tables[0];
 43                 }
 44             }
 45             catch (Exception ex)
 46             {
 47                 throw new Exception(ex.ToString());
 48             }
 49             finally
 50             {
 51                 con.Close();  //close database;
 52                 con.Dispose();// free connect
 53                 da.Dispose(); 
 54             }
 55             this.dataGridView1.Columns[0].HeaderText = "学号";
 56             this.dataGridView1.Columns[1].HeaderText = "姓名";
 57             this.dataGridView1.Columns[2].HeaderText = "性别";
 58             this.dataGridView1.Columns[3].HeaderText = "年龄";
 59             this.dataGridView1.Columns[4].HeaderText = "系别";
 60             for (int i = 0; i < 4; i++)
 61             {
 62                 this.dataGridView1.Columns[i].Width = 130;
 63             }
 64         }
 65 
 66         private void button1_Click(object sender, EventArgs e)
 67         {
 68             Form2 form2 = new Form2();
 69             form2.setParent(this);
 70             form2.Show();
 71         }
 72 
 73         private void button2_Click(object sender, EventArgs e)
 74         {
 75             Form3 form3 = new Form3();
 76             form3.setParent(this);
 77             form3.Show();
 78         }
 79 
 80         private void button3_Click(object sender, EventArgs e)
 81         {
 82             Form5 form5 = new Form5();
 83             form5.setParent(this);
 84             form5.Show();
 85         }
 86 
 87         private void Form1_Load(object sender, EventArgs e)
 88         {
 89             this.Location = new Point(300, 120);
 90             FormRefresh();
 91         }
 92 
 93         /// <summary>
 94         /// 用于form之间的刷新数据
 95         /// </summary>
 96         public void FormRefresh()
 97         {
 98             form1Refresh();
 99         }
100     }
101 }
View Code

4,添加学生信息

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Reflection;
 11 using System.Data.SqlClient;
 12 
 13 namespace WindowsFormsApplication2
 14 {
 15     public partial class Form2 : Form
 16     {
 17         public Form2()
 18         {
 19             InitializeComponent();
 20         }
 21          private Form1 frm_Parent;
 22 
 23         public void setParent(Form obj)
 24         {
 25             frm_Parent = (Form1)obj;
 26         }
 27 
 28         //数据库地址
 29         string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 30 
 31         private void Form2_Load(object sender, EventArgs e)
 32         {
 33             this.Location = new Point(300, 130);
 34             DataSet ds = new DataSet();
 35             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 36             SqlConnection con = new SqlConnection(connection);
 37 
 38             string sql = "select * from student";
 39             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 40             try
 41             {
 42                 da.Fill(ds);
 43                 if (ds.Tables[0].Rows.Count > 1)
 44                 {
 45                     dataGridView1.DataSource = ds.Tables[0];
 46                 }
 47             }
 48             catch (Exception ex)
 49             {
 50                 throw new Exception(ex.ToString());
 51             }
 52             finally
 53             {
 54                 con.Close(); //close database;
 55                 con.Dispose();// free connect
 56                 da.Dispose(); 
 57             }
 58             this.dataGridView1.Columns[0].Width = 120;
 59             this.dataGridView1.Columns[1].Width = 120;
 60             this.dataGridView1.Columns[2].Width = 120;
 61             this.dataGridView1.Columns[3].Width = 120;
 62             this.dataGridView1.Columns[4].Width = 120;
 63             this.dataGridView1.Columns[0].HeaderText = "学号";
 64             this.dataGridView1.Columns[1].HeaderText = "姓名";
 65             this.dataGridView1.Columns[2].HeaderText = "性别";
 66             this.dataGridView1.Columns[3].HeaderText = "年龄";
 67             this.dataGridView1.Columns[4].HeaderText = "系别";
 68         }
 69 
 70         private void button1_Click(object sender, EventArgs e)
 71         {
 72             try
 73             {
 74                 if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
 75                 {
 76                     MessageBox.Show("请输入完整的信息", "信息提示",
 77                     System.Windows.Forms.MessageBoxButtons.OK,
 78                     System.Windows.Forms.MessageBoxIcon.Warning);
 79                 }
 80                 else
 81                 {
 82                     DataSet ds = new DataSet();
 83                     string strsqls;
 84                     strsqls = string.Format("select * from student where sno='" + textBox1.Text.Trim() + "'");
 85                     SqlConnection con = new SqlConnection(connection);
 86                     SqlDataAdapter da = new SqlDataAdapter(strsqls, con);
 87                     da.Fill(ds);
 88                     if (ds.Tables[0].Rows.Count > 1)
 89                     {
 90                         //提示数据已存在
 91                         MessageBox.Show("该信息已存在,请重新输入", "信息提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
 92                     }
 93                     else
 94                     {
 95                         if (MessageBox.Show("确定要保存该条信息吗?", "信息提示", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
 96                         {
 97                             strsqls = string.Format("INSERT INTO student (sno,sname,ssex,sage,sdept)values('{0}','{1}','{2}','{3}','{4}')", textBox1.Text.Trim(), textBox2.Text.Trim(), textBox3.Text.Trim(), textBox4.Text.Trim(), textBox5.Text.Trim());
 98                             //定义连接对象
 99                             SqlConnection cons = new SqlConnection(connection);
100 
101                             SqlCommand cmd = new SqlCommand(strsqls, cons);
102                             try
103                             {
104                                 cons.Open();
105                                 cmd.ExecuteNonQuery();
106                                 if (cmd.ExecuteNonQuery() > 0)
107                                 {
108                                     MessageBox.Show("插入数据成功");
109                                 }
110                                 else
111                                 { 
112                                     MessageBox.Show("Fail");
113                                 }
114                             }
115                             catch { }
116                             finally
117                             {
118                                 cons.Close();
119                                 cons.Dispose();
120                                 cmd.Dispose();
121                             }
122                             MessageBox.Show("信息保存成功", "信息提示",
123                             System.Windows.Forms.MessageBoxButtons.OK,
124                             System.Windows.Forms.MessageBoxIcon.Information);
125                             //刷新数据
126                             string sqls = "select * from student order by sno";
127                             SqlDataAdapter dal = new SqlDataAdapter(sqls, con);
128                             dal.Fill(ds);
129                             if (ds.Tables[0].Rows.Count > 1)
130                             {
131                                 dataGridView1.DataSource = ds.Tables[0];
132                             }
133                         }
134                     }
135 
136                 }
137             }
138             catch { };
139         }
140 
141         //private void button2_Click(object sender, EventArgs e)
142         //{
143         //    DataSet ds = new DataSet();
144 
145         //    string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
146         //    SqlConnection con = new SqlConnection(connection);
147 
148         //    //string connString = "data source=.;" + "persist security info=true;" + "initial catalog=wisystem;" + "integrated security=SSPI;";
149         //    // SqlConnection con = new SqlConnection(connString);
150 
151         //    string sql = "select * from student";
152         //    SqlDataAdapter da = new SqlDataAdapter(sql, con);
153         //    try
154         //    {
155         //        da.Fill(ds);
156         //        if (ds.Tables[0].Rows.Count > 1)
157         //        {
158         //            dataGridView1.DataSource = ds.Tables[0];
159         //        }
160         //    }
161         //    catch (Exception ex)
162         //    {
163         //        throw new Exception(ex.ToString());
164         //    }
165         //    finally
166         //    {
167         //        con.Close(); //close database;
168         //        con.Dispose();// free connect
169         //        da.Dispose(); //
170         //    } 
171         //}
172 
173         private void textBox3_TextChanged(object sender, EventArgs e)
174         {
175             if (textBox3.Text.ToString() != "" || textBox3.Text.ToString() != "")
176             {
177                 MessageBox.Show("请输入男或女","提示信息");
178             }
179         }
180 
181         private void textBox4_TextChanged(object sender, EventArgs e)
182         {
183             MessageBox.Show("年龄默认0-120","提示信息");
184         }
185 
186         private void Form2_FormClosing(object sender, FormClosingEventArgs e)
187         {
188             frm_Parent.form1Refresh();
189         }
190 
191         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
192         {
193             int rowIndex  = dataGridView1.CurrentCell.RowIndex;
194             textBox1.Text = dataGridView1[0, rowIndex].Value.ToString();//学号
195             textBox3.Text = dataGridView1[2, rowIndex].Value.ToString();//性别
196             textBox4.Text = dataGridView1[3, rowIndex].Value.ToString();//年龄
197             textBox5.Text = dataGridView1[4, rowIndex].Value.ToString();//系别
198         }   
199     }
200 }
View Code

5,修改学生信息

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Reflection;
 11 using System.Data.SqlClient;
 12 using System.Diagnostics;
 13 
 14 namespace WindowsFormsApplication2
 15 {
 16     public partial class Form5 : Form
 17     {
 18         /*刷新付窗体*/
 19         private Form1 frm_Parent;
 20         public Form5()
 21         {
 22             InitializeComponent();
 23         }
 24         public void setParent(Form obj)
 25         {
 26             frm_Parent = (Form1)obj;
 27         }
 28 
 29         private int flag=0;
 30         private void Form5_Load(object sender, EventArgs e)
 31         {
 32             this.Location = new Point(300, 140);
 33             textBox1.Text = "按学号修改";
 34             textBox2.Text = "请选择一下要修改的字段";
 35             
 36         }
 37 
 38         private void textBox1_MouseClick(object sender, MouseEventArgs e)
 39         {
 40             textBox1.Text = string .Empty;
 41             textBox2.Text = string .Empty ;
 42         }
 43 
 44         private void checkBox1_CheckedChanged(object sender, EventArgs e)
 45         {
 46             flag=1;
 47         }
 48 
 49         private void checkBox2_CheckedChanged(object sender, EventArgs e)
 50         {
 51             flag = 2;
 52         }
 53 
 54         private void button1_Click(object sender, EventArgs e)
 55         {
 56             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 57             SqlConnection con = new SqlConnection(connection);
 58             con.Open();
 59             if ((flag == 1) && (flag != 2))
 60             {
 61                 string str = "UPDATE student SET sname='" + textBox2.Text.Trim() + "' WHERE sno='" + textBox1.Text.Trim() + "'";
 62                 SqlCommand cmd = new SqlCommand(str, con);
 63                 if (cmd.ExecuteNonQuery() > 0)
 64                 {
 65                     //System.Windows.Forms.MessageBox.Show("更新数据成功", "提示信息");
 66                 }
 67             }
 68             else if ((flag == 2) && (flag != 1))
 69             {
 70                 string str = "UPDATE student SET sage='" + textBox2.Text.Trim() + "' WHERE sno='" + textBox1.Text.Trim() + "'";
 71                 SqlCommand cmd = new SqlCommand(str, con);
 72                 
 73                 if (cmd.ExecuteNonQuery() > 0)
 74                 {
 75                     //System.Windows.Forms.MessageBox.Show("更新数据成功", "提示信息");
 76                 }
 77                 
 78                
 79             }
 80             else
 81                 //if ((flag == 1) && (flag == 2))
 82                 //{
 83                 //    string[] sArray = textBox2.Text.Split(' ');
 84                 //    string name = MessageBox.Show(sArray[0]).ToString();
 85                 //    string age = MessageBox.Show(sArray[1]).ToString();
 86 
 87                 //    string str = "UPDATE student SET sname='" + name + "',sage='" + age + "' WHERE sno='" + textBox1.Text.Trim() + "'";
 88                 //    SqlCommand cmd = new SqlCommand(str, con);
 89                 //    if (cmd.ExecuteNonQuery() > 0)
 90                 //    {
 91                 //        System.Windows.Forms.MessageBox.Show("更新数据成功", "提示信息");
 92                 //    }
 93                 //}
 94             if (textBox1.Text.ToString() == "")
 95             {
 96                 System.Windows.Forms.MessageBox.Show("请输入完整数据", "提示信息");
 97             }
 98             while (toolStripProgressBar1.Value < toolStripProgressBar1.Maximum)
 99             {
100                 toolStripProgressBar1.PerformStep();
101             }
102             System.Windows.Forms.MessageBox.Show("更新数据成","提示信息");
103         }
104 
105         private void Form5_FormClosing(object sender, FormClosingEventArgs e)
106         {
107             /*
108             Form1 form1 = new Form1(); //还是子窗体
109             form1.form1Refresh();
110             */
111             frm_Parent.form1Refresh();//父窗体的刷新
112         }
113     }
114 }
View Code

6,删除学生信息

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Threading.Tasks;
  9 using System.Windows.Forms;
 10 using System.Data .SqlClient;
 11 using System.Reflection;
 12 using System.Diagnostics;
 13 
 14 namespace WindowsFormsApplication2
 15 {
 16     public partial class Form3 : Form
 17     {
 18         /*刷新父窗体*/
 19         private Form1 frm_Parent;
 20 
 21         public Form3()
 22         {
 23             InitializeComponent();
 24         }
 25 
 26         public void setParent(Form obj)
 27         {
 28             frm_Parent = (Form1)obj;
 29         }
 30            
 31         private void Form3_Load(object sender, EventArgs e)
 32         {
 33             this.Location = new Point(300, 130);
 34             DataSet ds = new DataSet();
 35             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;"; 
 36             SqlConnection con = new SqlConnection(connection);
 37             string sql = "select * from student";
 38             SqlDataAdapter da = new SqlDataAdapter(sql, con);
 39             try
 40             {
 41                 da.Fill(ds);
 42                 if (ds.Tables[0].Rows.Count > 1)
 43                 {
 44                     dataGridView1.DataSource = ds.Tables[0];
 45                 }
 46             }
 47             catch (Exception ex)
 48             {
 49                 throw new Exception(ex.ToString());
 50             }
 51             finally
 52             {
 53                 con.Close(); //close database;
 54                 con.Dispose();// free connect
 55                 da.Dispose(); 
 56             }
 57            
 58             this.dataGridView1.Columns[0].HeaderText = "学号";
 59             this.dataGridView1.Columns[1].HeaderText = "姓名";
 60             this.dataGridView1.Columns[2].HeaderText = "性别";
 61             this.dataGridView1.Columns[3].HeaderText = "年龄";
 62             this.dataGridView1.Columns[4].HeaderText = "系别";
 63             for (int i = 0; i < 4; i++)
 64             {
 65                 this.dataGridView1.Columns[i].Width = 130;
 66             }
 67             
 68         }
 69         private void button1_Click(object sender, EventArgs e)
 70         {
 71             string connection = "Data Source=.;integrated security = SSPI;Initial Catalog=wisystem;";
 72             SqlConnection con = new SqlConnection(connection );
 73             con.Open();
 74             string strsql = string.Format("delete from student where sno='" + textBox1.Text.Trim() + "'");
 75             SqlCommand cmd=new SqlCommand (strsql,con);
 76             if (cmd.ExecuteNonQuery()>0)
 77             {
 78                 MessageBox.Show("删除数据成功","提示信息");
 79             }
 80             if (textBox1 .Text .ToString ()=="")
 81             {
 82                 System.Windows .Forms.MessageBox.Show("请输入完整数据","提示信息");
 83             }
 84             con.Close();
 85          }
 86       
 87         private void Form3_FormClosing(object sender, FormClosingEventArgs e)
 88         {
 89             /*
 90             Form1 form1 = new Form1(); //还是子窗体
 91             form1.form1Refresh();
 92             */
 93             
 94             frm_Parent.form1Refresh();//父窗体的刷新
 95         }
 96 
 97         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
 98         {
 99             
100             try
101             {
102                 string sno; 
103                 int rowIndex = dataGridView1.CurrentCell.RowIndex;
104                 textBox1.Text = dataGridView1[0, rowIndex].Value.ToString(); 
105                 sno = dataGridView1[0, rowIndex].Value.ToString();
106                 //MessageBox.Show(sno.ToString());
107             }
108             catch
109             {
110                 MessageBox.Show("删除失败","提示信息");
111             }
112 
113         }              
114     }
115 }
View Code

原文地址:https://www.cnblogs.com/wuzhang/p/wuzhang20131108.html