使用Datareader从数据库中读取数据 [英] Reading data from database using Datareader

查看:83
本文介绍了使用Datareader从数据库中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用c#中的DataReader从数据库表中检索一些数据。我的Tbl中有2列(userId和questionID列)我试图查询用户使用UserID和QuestionID尝试的每个问题。如果用户尝试了10个问题并且每个问题都有唯一ID,那么如何使用userID和QuestionID查询用户尝试的所有问题。

  String  questionid; 
Query = SELECT * FROM AnswersTable WHERE UserId ='1';
theReader = conn.ExecuteStatement(Query);
while (theReader.Read())
{


questionid = theReader [ QuestionId]。ToString();


}





所有10个问题ID都会存储在questionid变量中吗? br />


感谢您的帮助。

解决方案

theReader.Read()导致下一条记录被加载进入读者,这就是为什么你有一个while语句。所以,如果你有1个记录返回或10,000,调用.Read()将移动到下一个记录,直到它结束。


Quote:

是否所有10个QuestionsID都存储在 questionid 变量中?

否。这是因为它在while循环中被赋值。



这个for循环为 DataReader 将为所有循环记录一个接一个。这意味着当它循环查询返回的最后一条记录时,它只会将最后一个QuestionId分配给它。所以,最后你会看到最后 QuestionId 被分配给字符串变量 questionid



所以,你可以做的是,只需使用连接运算符 + = 并赋值。

< pre lang =cs> String questionid = string .Empty;
Query = SELECT * FROM AnswersTable WHERE UserId ='1';
theReader = conn.ExecuteStatement(Query);

while (theReader.Read())
{
questionid + = theReader [ QuestionId]。ToString()+ ;
}



所以,这会给你所有逗号分隔的逗号。



但这是不是一个好方法。你可能会尝试 DataAdapter ,它会立即将所有返回的数据分配给一个表。


列表与LT;串GT; questioned =  new  List< string>(); 
Query = SELECT * FROM AnswersTable WHERE UserId ='1';
theReader = conn.ExecuteStatement(Query);
while (theReader.Read())
{
questioned.Add(theReader [ QuestionId]。ToString());
}

// 并访问此类ID,
字符串 id =质疑[ 1 ];

< / string > < / string >



而不是逗号,或其他任何东西,而不是我更喜欢在一些集合中存储多个项目,即Array,List< t>等..



-KR


Hi,
I am trying to retrieve some data from database table using a DataReader in c#. I have got 2 columns in my Tbl (userId and questionID columns )I am trying to query every single question that was attempted by the user using the UserID and QuestionID. If the user attempted 10 questions and each question has a unique ID how will able to query the all the questions attempted by the user using the userID and QuestionID.

String questionid;
Query = "SELECT * FROM AnswersTable WHERE UserId = '1'";
           theReader = conn.ExecuteStatement(Query);
           while (theReader.Read())
           {


               questionid = theReader["QuestionId"].ToString();
               

           }



Will all the 10 questions ID's get stored in the questionid variable?

Thank you for you help.

解决方案

theReader.Read() causes the next record to be loaded into the reader which is why you have it in a while statement. So, if you have 1 record returned or 10,000, calling .Read() will move to the next record until it gets to the end.


Quote:

Will all the 10 QuestionsIDs get stored in the questionid variable?

No. That is because it is assigned value inside the while loop.

This while loop for DataReader will loop for all the records one by one. That means when it loops for the last record returned by Query, it will just assign the last QuestionId to that. So, at last you will see that last QuestionId is assigned to the string variable questionid.

So, what you can do is, just use concatenation operator += and assign value.

String questionid = string.Empty;
Query = "SELECT * FROM AnswersTable WHERE UserId = '1'";
theReader = conn.ExecuteStatement(Query);

while (theReader.Read())
{
   questionid += theReader["QuestionId"].ToString() + ",";
}


So, this will give you all Ids comma separated.

But this is not a good approach. You would probably try DataAdapter, which assigns all the returned data to a Table at once.


List<string> questioned = new List<string>();
Query = "SELECT * FROM AnswersTable WHERE UserId = '1'";
theReader = conn.ExecuteStatement(Query);
while (theReader.Read())
{
    questioned.Add(theReader["QuestionId"].ToString());
}

// and access any id like this,
String id = questioned[1];

</string></string>


Instead of comma sperated, or anything else, rather I'd prefer to store multiple items in some collection i.e Array, List<t> etc..

-KR


这篇关于使用Datareader从数据库中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆