C#中存储的信息

C#中存储的信息

本文介绍了访问Array C#中存储的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在我的数据库中有两个表,我在其中查询,一个名为QuestionsTable和AnswersTable。



我从AnswersTable查询questionID并将ID存储在字符串数组中。像这样:

Hi All,
I have got two table in my database in which I am querying from, one named QuestionsTable and AnswersTable.

I am querying questionIDs from the AnswersTable and storing the ID's in a string array. like this:

List<string> questionId  = new List<string>();


       private void button1_Click(object sender, EventArgs e)
       {
           Query = "SELECT * FROM TblAnswers WHERE UserId = '1'";
           theReader = conn.ExecuteStatement(Query);
           while (theReader.Read())
           {

               questionId.Add(theReader["QuestionId"].ToString());


           }



要访问存储在此阵列中的这些ID,我这样做:




To access these ID's stored in this array, I am doing this:

String Id = questionId[0];


           Query = "SELECT *  FROM QuestionsTable WHERE Id = '" + Id + "'";
           theReader = conn.ExecuteStatement(Query);
           while(theReader.Read())
           {
              question = theReader["Question"].ToString();

           }







如果上述情况我只查询一个问题尝试用户1,但我的主要问题是,让用户1尝试10个问题,我想查询并从字符串数组中访问10个问题的ID,我是否必须定义10个变量才能访问每个QuestionID?例如,我必须这样做:






In case described above I am only querying one question that "User 1" attempted but my main issue is that , lets that "User 1" attempted 10 questions and I would like to query and access the 10 questions' ID's from the string array, will I have to define 10 variables in order to access each and every QuestionID? e.g will I have to do this:

String Id = questionId[0];
String Id2 = questionId[1];
String Id3 = questionId[2];
String Id4 = questionId[3];



等等,最多10个......



我是否有更简单的方法可以在不定义10个变量的情况下访问这些ID?



我希望这是有道理的,谢谢。


and so on up to 10...

Isn't there an easier way I can access these ID's without defining 10 variables?

I hope this makes sense and thank you.

推荐答案

Query = "SELECT *  FROM QuestionsTable WHERE Id = '" + questionId[nnn] + "'";



如果你想为每个人做同样的操作问题,然后把它放到一个循环中:


If you want to do the identical operation for each of the questions, then put it into a loop:

foreach (string id in questionId)
{
  Query = "SELECT *  FROM QuestionsTable WHERE Id = '" + id + "'";
  theReader = conn.ExecuteStatement(Query);
  while(theReader.Read())
  {
    question = theReader["Question"].ToString();
  }
}


string[] ids = questionId.ToArray();



请参阅: []。



-SA


这篇关于访问Array C#中存储的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:19