private void button1_Click(object sender, EventArgs e) { 主页 fm = new 主页(); fm.Show(); this.Hide(); }
private void button1_Click(object sender, EventArgs e) { try { string UserName = textBox1.Text.Trim(); string PassWord = textBox2.Text.Trim(); if (UserName == "") { MessageBox.Show("用户名为空,请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (PassWord == "") { MessageBox.Show("密码为空,请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string str = "SELECT count(*) from Users WHERE username= '******' and password= '******'"; string connectionStr = "Data Source=MYCOMPUTER\\SQLEXPRESS;Integrated Security=True"; SqlConnection conn = new SqlConnection(connectionStr); //创建链接对象 SqlCommand cmd = new SqlCommand(str, conn); //创建命令对象 conn.Open(); //如果一个连接对象打开,那么必须在try最后关闭它 Console.WriteLine("数据连接成功打开"); SqlDataReader sdr = cmd.ExecuteReader(); //创建数据读取器对象 sdr.Read(); sdr.Close(); int n = (int)cmd.ExecuteScalar();//传回第一行,赋给n if (n >= 1) { 主页 fm = new 主页(); fm.Show(); this.Hide(); } else { MessageBox.Show("用户名或密码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);//密码错误 } } } catch (Exception ex) { MessageBox.Show(ex.Message);//打印异常 } }