C# 数据库的显示值
我有这样的代码C# 数据库的显示值,c#,sql,C#,Sql,我有这样的代码 Global.dbCon.Open(); int idQuestion; kalimatSql = kalimatSql; Global.reader = Global.riyeder(kalimatSql); if (Global.reader.HasRows) { while (Global.reader.Read()) { idQuestion = Convert.ToInt32(Global.reader.GetValue(0)); //
Global.dbCon.Open();
int idQuestion;
kalimatSql = kalimatSql;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
//messageBox.Show(idQuestion.ToString()); -->first message box
}
}
//messageBox.Show(idQuestion.ToString()); -->second message box
Global.dbCon.Close();
我没有问题显示第一个messagebox,但是如何显示第二个messagebox
已编辑 我试图将@rhughes中的代码变成一个函数(类),如下所示
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach (int id in idQuestions) {
return id;
}
Global.dbCon.Open();
列表ID问题=新列表();
Global.reader=Global.riyeder(kalimatSql);
if(Global.reader.HasRows){
while(Global.reader.Read()){
int idQuestion=Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach(idQuestions中的int-id){
返回id;
}
但这不起作用,因为并非所有代码路径都返回一个值……我想知道如何正确地执行此操作?尝试以下方法:
Global.dbCon.Open();
List<int> idQuestions = new List<int>();
kalimatSql = kalimatSql;
Global.reader = Global.riyeder(kalimatSql);
if (Global.reader.HasRows) {
while (Global.reader.Read()) {
int idQuestion = Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach (int id in idQuestions)
{
messageBox.Show(id.ToString());
}
Global.dbCon.Open();
列表ID问题=新列表();
kalimatSql=kalimatSql;
Global.reader=Global.riyeder(kalimatSql);
if(Global.reader.HasRows){
while(Global.reader.Read()){
int idQuestion=Convert.ToInt32(Global.reader.GetValue(0));
idQuestions.Add(idQuestion);
}
}
Global.dbCon.Close();
foreach(idQuestions中的int-id)
{
Show(id.ToString());
}
我们在这里做的是将所有问题ID添加到一个列表中,然后再显示每个问题ID。问题出在哪里?@Selman22 display second messagebox您可以说您希望在第二个messagebox中显示什么@Selman22他做到了-//messageBox.Show(idQuestion.ToString());-->第二条信息box@rhughes不清楚他想要什么。他是想显示最后一个idQuestion,还是想用一个循环显示所有idQuestions,还是想用一个MessageBox同时显示所有idQuestions?我试图修改你的代码,使其成为一个函数……但结果是错误的,你能给我举个例子说明如何正确地执行吗?@katik Sure。如果你创建了一个新问题,我们可以在那里解决它。我已经创建了关于它的新问题。。。。