写法一:
#region
//增加区分代码块
if (this.radteacher.Checked)//选择单选按钮
{
String constr = "server=127.0.0.1;userid=root;password=;database=dormdb_a133;";
MySqlConnection conn = new MySqlConnection(constr);//连接数据库
conn.Open();
string sql = String.Format("select * from teacher where teachusername = '{0}' and teachpass = '{1}'", txtname.Text.Trim(), txtpass.Text.Trim());//查询语句
MySqlCommand comm = new MySqlCommand(sql, conn);//执行查询
MySqlDataReader reader = comm.ExecuteReader();//返回查询结果
if (reader.HasRows)//如果取得数据
{
//MessageBox.Show("success");
this.Hide();//隐藏当前窗口
dormmanagement dor = new dormmanagement();//开启新窗口
dor.Show();
}
else
{
MessageBox.Show("fail");
}
}
#endregion
写法二 :添加DBhelper类