c#连接数据库查询时 出现这样的错误
我把从数据库中查询出来的东西要放在listview中显示方法如下publicvoidfilllist(){stringquestion="";stringchoicea=...
我把从数据库中查询出来的东西要放在listview中显示
方法如下
public void filllist()
{
string question = "";
string choicea = "";
string choiceb = "";
string choicec = "";
string choiced = "";
string answer = "";
string difficulty = "";
try
{
string sql = string.Format("select id,question,choicea,choiceb,choicec,choiced,answer,difficulty from table_question where difficulty='{}' ", this.textBox1.Text.Trim());
SqlCommand command = new SqlCommand(sql, user.connection);
user.connection.Open();
SqlDataReader datareader = command.ExecuteReader();
this.listView1.Items.Clear();
if (!datareader.HasRows)
{
MessageBox.Show("抱歉,没有找到符合条件的试题", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
while (datareader.Read())
{
question = datareader["question"].ToString();
choicea = datareader["choicea"].ToString();
choiceb = datareader["choiceb"].ToString();
choicec = datareader["choicec"].ToString();
choiced = datareader["choiced"].ToString();
answer = datareader["answer"].ToString();
difficulty = datareader["difficulty"].ToString();
ListViewItem lvi = new ListViewItem(question);
this.listView1.Items.Add(lvi);
lvi.Tag = (int)datareader["id"];
lvi.SubItems.AddRange(new string[] {question,choicea,choiceb,choicec,choiced,answer,difficulty});
}
}
datareader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
user.connection.Close();
}
}
可是当调用执行的时候提示“输入字符串的格式不正确”
请问大虾 这样的错误提示出现的原因一般是什么 如果能找到我的问题更好了 谢谢
没有显示行呀 错误后进入catch语句,是以messagebox出现错误提示的 呜呜呜 郁闷死我了 展开
方法如下
public void filllist()
{
string question = "";
string choicea = "";
string choiceb = "";
string choicec = "";
string choiced = "";
string answer = "";
string difficulty = "";
try
{
string sql = string.Format("select id,question,choicea,choiceb,choicec,choiced,answer,difficulty from table_question where difficulty='{}' ", this.textBox1.Text.Trim());
SqlCommand command = new SqlCommand(sql, user.connection);
user.connection.Open();
SqlDataReader datareader = command.ExecuteReader();
this.listView1.Items.Clear();
if (!datareader.HasRows)
{
MessageBox.Show("抱歉,没有找到符合条件的试题", "查询提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
while (datareader.Read())
{
question = datareader["question"].ToString();
choicea = datareader["choicea"].ToString();
choiceb = datareader["choiceb"].ToString();
choicec = datareader["choicec"].ToString();
choiced = datareader["choiced"].ToString();
answer = datareader["answer"].ToString();
difficulty = datareader["difficulty"].ToString();
ListViewItem lvi = new ListViewItem(question);
this.listView1.Items.Add(lvi);
lvi.Tag = (int)datareader["id"];
lvi.SubItems.AddRange(new string[] {question,choicea,choiceb,choicec,choiced,answer,difficulty});
}
}
datareader.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
user.connection.Close();
}
}
可是当调用执行的时候提示“输入字符串的格式不正确”
请问大虾 这样的错误提示出现的原因一般是什么 如果能找到我的问题更好了 谢谢
没有显示行呀 错误后进入catch语句,是以messagebox出现错误提示的 呜呜呜 郁闷死我了 展开
2个回答
展开全部
sql错了吧???
string sql = string.Format("select id,question,choicea,choiceb,choicec,choiced,answer,difficulty from table_question where difficulty='{}' ", this.textBox1.Text.Trim());
应该是where difficulty='{0}' "
string sql = string.Format("select id,question,choicea,choiceb,choicec,choiced,answer,difficulty from table_question where difficulty='{}' ", this.textBox1.Text.Trim());
应该是where difficulty='{0}' "
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你要说一下异常发生在哪一行 不知道我的话我也不知道啊
像你这个错误一般都是在int.Parse的时候出现的,比如你想把字符串转换成int,但是字符串看起来并不像一个可以转换成int的东西,他就会报这个异常
像你这个错误一般都是在int.Parse的时候出现的,比如你想把字符串转换成int,但是字符串看起来并不像一个可以转换成int的东西,他就会报这个异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询