public void addCourseStu() { try { string strCon = "server=127.0.0.1;uid=sa;pwd=123456;database=ClassAttendanceManageSystem"; SqlConnection connection = new SqlConnection(strCon); DataSet myDataSet = new DataSet(); string sql = "Select *from studentInfo"; string str = "insert into studentInfo VALUES ('" + id + "','" + name + "','" + sex + "','" + cid + "','" + cname + "','" + cardid + "','" + strcourseid + "','" + psw + "')"; connection.Open(); SqlCommand xg; xg = new SqlCommand(str, connection); if (xg.ExecuteNonQuery() > 0) //判断是否成功 { //执行要操作的语句 SqlDataAdapter mycommand = new SqlDataAdapter(sql, connection); myDataSet.Clear(); mycommand.Fill(myDataSet, "studentInfo"); MessageBox.Show("选课成功!请刷新!"); this.Close(); Form_studentMain a = new Form_studentMain(); a.ShowDialog(); // Form_teacherMain.dataGridView1.DataSource = myDataSet.Tables["courseInfo"]; } connection.Close(); } catch (Exception x) { MessageBox.Show("error!" + x.ToString()); } }
private void button_login_Click(object sender, EventArgs e) { //这里写入刚刚的判断语句 if (!judge()) { MessageBox.Show("账号或密码为空,请正确输入信息", "提示"); return; } try { string strCon = "server=127.0.0.1;uid=sa;pwd=123456;database=ClassAttendanceManageSystem"; SqlConnection connection = new SqlConnection(strCon); DataSet myDataSet = new DataSet(); if (flag == 1) //radioButton1_stu 判断是否为学生用户 { string sql = "Select *from studentInfo where stuID ='" + textBox1.Text.Trim() + "' and stuPassword='******'"; connection.Open(); SqlDataAdapter mycommand = new SqlDataAdapter(sql, connection); myDataSet.Clear(); mycommand.Fill(myDataSet, "studentInfo"); connection.Close(); if (myDataSet.Tables["studentInfo"].Rows.Count > 0) { MessageBox.Show("登录成功!", "提示"); this.Visible = false; Form_studentMain stu = new Form_studentMain(); str = textBox1.Text; //注意,这句不能放在stu.Show();的后面,否则将传值不到其他窗体;会先执行textBox2.Text =stu.str;再执行str = textBox1.Text; str1 = textBox2.Text; //保存密码 stu.Show(); } else { MessageBox.Show("用户名或密码错误!", "提示"); } } else if (flag == 2) { string sql = "Select *from teacherInfo where teacherID ='" + textBox1.Text.Trim() + "' and teacherPassword='******'"; connection.Open(); SqlDataAdapter mycommand = new SqlDataAdapter(sql, connection); myDataSet.Clear(); mycommand.Fill(myDataSet, "teacherInfo"); connection.Close(); if (myDataSet.Tables["teacherInfo"].Rows.Count > 0) { MessageBox.Show("登录成功!", "提示"); this.Visible = false; Form_teacherMain a = new Form_teacherMain(); str = textBox1.Text; //注意,这句不能放在f2.ShowDialog();的后面,否则会先执行textBox2.Text = f1.str;再执行str = textBox1.Text; str1 = textBox2.Text; //保存密码 a.ShowDialog(); } else { MessageBox.Show("用户名或密码错误!", "提示"); } } else if (flag == 3) { string sql = "Select *from adminInfo where adminID ='" + textBox1.Text.Trim() + "' and adminPassword='******'"; connection.Open(); SqlDataAdapter mycommand = new SqlDataAdapter(sql, connection); myDataSet.Clear(); mycommand.Fill(myDataSet, "adminInfo"); connection.Close(); if (myDataSet.Tables["adminInfo"].Rows.Count > 0) { MessageBox.Show("登录成功!", "提示"); this.Visible = false; Form_adminMain a = new Form_adminMain(); str = textBox1.Text; //注意,这句不能放在f2.ShowDialog();的后面,否则会先执行textBox2.Text = f1.str;再执行str = textBox1.Text; str1 = textBox2.Text; //保存密码 a.ShowDialog(); } else { MessageBox.Show("用户名或密码错误!", "提示"); } } else { MessageBox.Show("请选择用户类型!", "提示"); } } catch (Exception x) { MessageBox.Show("error!" + x.ToString()); } }