大家帮忙看下是什么问题
小妖刀 2009-12-22 03:16:37 直接跳入catch中,没有执行while循环。
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入要查询的学员姓名", "输入提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox1.Focus();
}
else
{
FillListView();
}
}
private void FillListView()
{
string id;
string name;
string add;
string sex;
try
{
string sql = string.Format("select id,name,address,sex from student where name like'%{0}%'", textBox1.Text);
SqlCommand command = new SqlCommand(sql, DBHelper.connection);
SqlDataReader dataReader;
DBHelper.connection.Open();
dataReader = command.ExecuteReader();
listView1.Items.Clear();
if (!dataReader.HasRows)
{
MessageBox.Show("抱歉,没有你要查找的用户","消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
while (dataReader.Read())
{
id = (string)dataReader["id"];
name = (string)dataReader["name"];
add = (string)dataReader["address"];
sex = (string)dataReader["sex"];
MessageBox.Show("cuowu");
ListViewItem lviStudent = new ListViewItem(id);
lviStudent.Tag = (int)dataReader["id"];
listView1.Items.Add(lviStudent);
lviStudent.SubItems.AddRange(new string[] { name, add, sex });
}
}
dataReader.Close();
}
catch
{
MessageBox.Show("查询数据库出错", "消息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
DBHelper.connection.Close();
}
}