111,129
社区成员
发帖
与我相关
我的任务
分享//查询老师用户
private void selectTeacher()
{
int TeacherId, UserStateId = -1; //教师号 用户状态
string LoginId, LoginPwd, TeacherName, Sex, Birthday, phone, Email; //用户名,密码,姓名,性别,生日,电话,EMail
string a1 = textBox1.Text == "" ? null : textBox1.Text;
string a2 = textBox2.Text == "" ? null: textBox2.Text;
//模糊和多重查询
try
{
//连接查询
string sql = string.Format("select * from Teacher where TeacherId like '%{0}%' and LoginId like '%{1}%'", a1, a2);
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlCommand command = new SqlCommand(sql, SQL.connection);
//MessageBox.Show("11", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SQL.connection.Open();
//MessageBox.Show("111", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlDataReader datareader = command.ExecuteReader(); //执行查询语句
//MessageBox.Show("1111", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listView2.Items.Clear(); //清空listView2列表
if (!datareader.HasRows)
{
MessageBox.Show("抱歉没有找到你想要的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
while (datareader.Read())
{
TeacherId = (int)datareader["TeacherId"]; //教师号
UserStateId = (int)datareader["UserStateId"]; //用户状态Id
string UserState = UserStateId == 1 ? "活动" : "非活动"; //用户状态
LoginId = (string)datareader["LoginId"]; //用户名
LoginPwd = (string)datareader["LoginPwd"]; //密码
TeacherName = (string)datareader["TeacherName"]; //姓名
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Sex = (string)datareader["Sex"]; //性别
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这里出了问题 数据库中的类型为datatime
Birthday = Convert.ToString(datareader["Birthday"]); //生日
MessageBox.Show(Convert.ToString(datareader["Birthday"]), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
phone = (string)datareader["phone"]; //电话
Email = (string)datareader["Email"]; //Email
//创建一个listView项
ListViewItem Teacher = new ListViewItem(Convert.ToString(TeacherId));
//向listView中添加一个新项
listView1.Items.Add(Teacher);
//添加子项
Teacher.SubItems.AddRange(new string[] { LoginId, LoginPwd, UserState, TeacherName, Sex, Birthday, phone, Email });
//MessageBox.Show(Convert.ToString(StudentId), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
datareader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("查询数据库出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
SQL.connection.Close();
}
}