ExecuteReader 仅返回 1 个值
当 QuestionnaireID 与我的查询匹配时,我使用 ExecuteReader 返回 QuestionText 的值。 (请参阅数据库设计)
但是我的问题是,当我运行该项目时,我的 ExecuteReader 仅返回输入的最后一个值。例如,如果我要在 QuestionnaireID 1 下创建 3 个问题(A、B、C)。ExecuteReader 将仅返回问题 C。
如何返回 QuestionText 列中具有相同调查问卷 ID 的所有值?
数据库设计
****.cs**
public string GetQuestionName(int QuestionnaireID)
{
string returnvalue = string.Empty;
SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
myCommand.Parameters[0].Value = QuestionnaireID;
_productConn.Open();
SqlDataReader test = myCommand.ExecuteReader();
while (test.Read())
{
returnvalue = test.GetString(0);
}
_productConn.Close();
return returnvalue;
}
存储过程 。
USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hbo].[GetQuestion] Script Date: 11/12/2011 13:12:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[GetQuestion]
(
@QUEST_ID int
)
AS
/*SET NOCOUNT ON;*/
SELECT QuestionText FROM [Questions] WHERE QuestionnaireID = @QUEST_ID
RETURN
提前致谢
I am using an ExecuteReader to return the values of QuestionText when QuestionnaireID matches my query. (See Database Design)
However my problem is that when I run the project my ExecuteReader only returns the last value that was input. For example, if I was to create 3 questions (A,B,C) under the questionnaireID of 1. The ExecuteReader would return Question C only.
How can I return all values in the QuestionText collumn that have the same questionnaireID?
Database Design
****.cs**
public string GetQuestionName(int QuestionnaireID)
{
string returnvalue = string.Empty;
SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
myCommand.Parameters[0].Value = QuestionnaireID;
_productConn.Open();
SqlDataReader test = myCommand.ExecuteReader();
while (test.Read())
{
returnvalue = test.GetString(0);
}
_productConn.Close();
return returnvalue;
}
Stored Procedure
USE [devworks_oscar]
GO
/****** Object: StoredProcedure [hbo].[GetQuestion] Script Date: 11/12/2011 13:12:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [hgomez].[GetQuestion]
(
@QUEST_ID int
)
AS
/*SET NOCOUNT ON;*/
SELECT QuestionText FROM [Questions] WHERE QuestionnaireID = @QUEST_ID
RETURN
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的读取器正在返回所有值,但代码 -
将继续使用从数据读取器返回的下一个值覆盖
returnvalue
变量,因此您只会留下最后一个值。您可以创建一个字符串列表 -
然后添加返回到列表的每个值 -
然后从函数返回列表而不是字符串。
Your reader is returning all the values but the code -
will keep overwriting the
returnvalue
variable with the next value returned from the datareader so you will only be left with the last value.You could create a list of strings -
then add each value returned to the list -
then return the list from your function instead of a string.
使用数据读取器,您仅选取一个问题名称,并在每次读取时覆盖其值 - 然后返回最后一项。相反,只需使用
List
来保存所有值并返回:Using the data reader you pick up one question name only and override its value on each read - then you return that last item. Instead just use a
List<string>
to hold all your values and return that:这就是你的问题。您将循环多次,每次都用最后一个值覆盖它,然后返回一次。
您需要一个列表和 .add() 。
There's your problem. You're looping that multiple times, each time overwriting it with the last value, then returning once.
You want a list and .add() instead.