简体   繁体   English

使用for循环将数据库记录存储到数组中 C#

[英]Storing database records into array with a for loop | C#

I am creating an online test software using C# winforms and SQL Server. 我正在使用C#winforms和SQL Server创建一个在线测试软件。 Now I have two tables (SQL server) one of is a question , and one is the question_answers . 现在我有两个表(SQL服务器),其中一个是问题 ,一个是question_answers I am inserting the questions and their answers from the database to the array. 我将问题及其答案从数据库插入到数组中。 I have the code something like this. 我有这样的代码。

public dbtest()
        {
            sqlconnection = new SqlConnection(ConnectionString);
            Query =
                "SELECT questions.id as qid, questions.question as qq FROM dbo.questions; ";
            sqlcommand = new SqlCommand(Query, sqlconnection);
            sqlconnection.Open();
            SqlDataReader sdr = sqlcommand.ExecuteReader();

            while (sdr.Read())
            {
                int quiz_id;
                bool quiz_id1 = Int32.TryParse(sdr["qid"].ToString(), out quiz_id);

                sqlconnection = new SqlConnection(ConnectionString);
                Query =
                "SELECT id, question_id, answer, is_correct FROM dbo.question_answers WHERE question_id = " + quiz_id + "  ; ";
                sqlcommand = new SqlCommand(Query, sqlconnection);
                sqlconnection.Open();
                SqlDataReader answr = sqlcommand.ExecuteReader();

                while (answr.Read())
                {
                    questions[quiz_id, 0] = sdr["qq"].ToString();
                    for (int ii = 1; ii < 5; ii++)
                    {
                        questions[quiz_id, ii] = answr["answer"].ToString();
                    }
                }
            }
        }

I need an array to do like this. 我需要一个像这样做的数组。


            questions[0, 0] = "The 2006 World Cup Football Tournament held in";
            questions[0, 1] = "France";
            questions[0, 2] = "China";
            questions[0, 3] = "*Germany";
            questions[0, 4] = "Brazil";
            //
            questions[1, 0] = "The 'Black flag' signifies";
            questions[1, 1] = "revolution/danger";
            questions[1, 2] = "*protest";
            questions[1, 3] = "truce";
            questions[1, 4] = "peace";
            //
            questions[2, 0] = "Robert Koch worked on";
            questions[2, 1] = "*tuberculosis";
            questions[2, 2] = "cholera";
            questions[2, 3] = "malaria";
            questions[2, 4] = "diabetes";

But when I use for loop , I get the same answer records like this. 但是当我使用for循环时 ,我会得到相同的答案记录。


            questions[0, 0] = "The 2006 World Cup Football Tournament held in";
            questions[0, 1] = "France";
            questions[0, 2] = "France";
            questions[0, 3] = "France";
            questions[0, 4] = "France";
            //

I am new in C# winfroms programming. 我是C#winfroms编程的新手。 And I can not get better algorithm. 我无法获得更好的算法。 So I have two questions. 所以我有两个问题。 The first one: How can I fix this code's problem? 第一个:如何修复此代码的问题?

                {
                    questions[quiz_id, 0] = sdr["qq"].ToString();
                    for (int ii = 1; ii < 5; ii++)
                    {
                        questions[quiz_id, ii] = answr["answer"].ToString();
                    }
                }

The second: Are there other optimally solutions to get questions and their answers from database? 第二:是否有其他最佳解决方案可以从数据库中获取问题及其答案? Excuse me if I can not explain. 如果我无法解释,请原谅。 Thanks for answers! 谢谢你的回答!

the following code : 以下代码:

for (int ii = 1; ii < 5; ii++)
{
    questions[quiz_id, ii] = answr["answer"].ToString();
}

will set the same answer in the sub array questions [0, X] with X between 1 and 4. 将在子数组问题[0,X]中设置相同的答案,其中X在1和4之间。
And then another answer in the sub array questions [1, X] with X between 1 and 4. 然后子数组中的另一个答案问题[1,X],X在1和4之间。
And then another answer in the sub array questions [2, X] with X between 1 and 4. 然后子数组中的另一个答案问题[2,X],X在1到4之间。

You need to get rid of that unnecessary loop. 你需要摆脱那个不必要的循环。

public dbtest(){
        sqlconnection = new SqlConnection(ConnectionString);
        Query =
            "SELECT questions.id as qid, questions.question as qq FROM dbo.questions; ";
        sqlcommand = new SqlCommand(Query, sqlconnection);
        sqlconnection.Open();
        SqlDataReader sdr = sqlcommand.ExecuteReader();

        while (sdr.Read())
        {
            int quiz_id;
            bool quiz_id1 = Int32.TryParse(sdr["qid"].ToString(), out quiz_id);

            sqlconnection = new SqlConnection(ConnectionString);
            Query =
            "SELECT id, question_id, answer, is_correct FROM dbo.question_answers WHERE question_id = " + quiz_id + "  ; ";
            sqlcommand = new SqlCommand(Query, sqlconnection);
            sqlconnection.Open();
            SqlDataReader answr = sqlcommand.ExecuteReader();
            questions[quiz_id, 0] = sdr["qq"].ToString();
            var ii = 1;
            while (answr.Read())
            {
                questions[quiz_id, ii] = answr["answer"].ToString();
                ii++;
            }
        }
    }

You could use SQL join to map question and answer and just transform the result set into an array of some sort. 您可以使用SQL连接来映射问题和答案,然后将结果集转换为某种数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM