大家帮忙看下是什么问题

小妖刀 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();
}
}
...全文
97 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbaiwu 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 foryouwife 的回复:]
("select id,name,address,sex from student where name like'%{0}%'"
like  后面加空格
[/Quote]

正解
angel6709 2009-12-23
  • 打赏
  • 举报
回复
哎,不会调试,更不要说log了
foryouwife 2009-12-23
  • 打赏
  • 举报
回复
("select id,name,address,sex from student where name like'%{0}%'"
like 后面加空格
abcdef1111111 2009-12-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 liu_haiyang 的回复:]
  直接跳入catch中,没有执行while循环。
 [/Quote]原因很简单,程序没运行到while这一行就已经出错了,然后就直接跳到catch了,楼主在FillListView() 方法那设置一个断点,然后单步执行,看看到那一行跳转的
哈喽Baby 2009-12-22
  • 打赏
  • 举报
回复
设断点,看看从哪句代码跑到catch里勒
Tiantiandiandian 2009-12-22
  • 打赏
  • 举报
回复
把catch到的信息log出来
cuike519 2009-12-22
  • 打赏
  • 举报
回复
执行while之前就有异常产生了当然跳到catch了
小妖刀 2009-12-22
  • 打赏
  • 举报
回复
没有错误提示啊 可以运行,可是查不出来。直接就跳到catch中。
hhc123 2009-12-22
  • 打赏
  • 举报
回复
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
}
这句没看出错误来啊,贴错误出来吧

天乐 2009-12-22
  • 打赏
  • 举报
回复
可能的原因有很多,你最好把出错的提示贴出来

类似这种错误,最好的办法是跟踪调试,单步执行,看看到底是那一条语句执行出错,

跟踪到了代码,一般都能自己解决
ivws_19 2009-12-22
  • 打赏
  • 举报
回复
调试一下,看是哪句出的异常

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧