如何在for循环里用参数方法读取数据库中数据
SqlDataReaderreader=null;SqlCommandcmd=conn.CreateCommand();for(inti=0;i<10;i++){cmd....
SqlDataReader reader = null;
SqlCommand cmd = conn.CreateCommand();
for (int i = 0; i < 10; i++)
{
cmd.CommandText = "Select Answer from Question where QuestionId=@QUESTIONID";
cmd.Parameters.Add(new SqlParameter("@QUESTIONID", question[i]));
reader = cmd.ExecuteReader();
while (reader.Read())
{
ans[i] = reader.GetValue(0).ToString();
}
reader.Close();
}
在运行一次后出现异常:"参数@QUESTIONID已声明,变量在查询批次里必须唯一",
有什么方法可以在for循环里用参数方法读取数据库中数据呢?? 展开
SqlCommand cmd = conn.CreateCommand();
for (int i = 0; i < 10; i++)
{
cmd.CommandText = "Select Answer from Question where QuestionId=@QUESTIONID";
cmd.Parameters.Add(new SqlParameter("@QUESTIONID", question[i]));
reader = cmd.ExecuteReader();
while (reader.Read())
{
ans[i] = reader.GetValue(0).ToString();
}
reader.Close();
}
在运行一次后出现异常:"参数@QUESTIONID已声明,变量在查询批次里必须唯一",
有什么方法可以在for循环里用参数方法读取数据库中数据呢?? 展开
1个回答
展开全部
for (int i = 0; i < 10; i++)
{
SqlDataReader reader = null;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select Answer from Question where QuestionId=@QUESTIONID";
cmd.Parameters.Add(new SqlParameter("@QUESTIONID", question[i]));
reader = cmd.ExecuteReader();
while (reader.Read())
{
ans[i] = reader.GetValue(0).ToString();
}
reader.Close();
}
{
SqlDataReader reader = null;
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Select Answer from Question where QuestionId=@QUESTIONID";
cmd.Parameters.Add(new SqlParameter("@QUESTIONID", question[i]));
reader = cmd.ExecuteReader();
while (reader.Read())
{
ans[i] = reader.GetValue(0).ToString();
}
reader.Close();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询