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

时间:2019-06-02 15:33:25

标签: c# arrays .net sql-server winforms

我正在使用C#winforms和SQL Server创建一个在线测试软件。现在,我有两个表(SQL Server),其中一个是问题,另一个是 question_answers 。 我将问题及其答案从数据库插入到数组中。 我有类似这样的代码。

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();
                    }
                }
            }
        }

我需要一个数组来做到这一点。


            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";

但是当我使用 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";
            //

我是C#winfroms编程的新手。而且我无法获得更好的算法。 所以我有两个问题。 第一个:如何解决此代码的问题?

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

第二个:还有其他最佳解决方案可以从数据库中获取问题及其答案? 对不起,如果我无法解释。感谢您的回答!

1 个答案:

答案 0 :(得分:0)

以下代码:

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

将在X介于1和4之间的子数组问题[0,X]中设置相同的答案。
然后是子数组问题[1,X]中的另一个答案,其中X在1到4之间。
然后是子数组问题[2,X]中的另一个答案,其中X在1到4之间。

您需要摆脱不必要的循环。

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++;
            }
        }
    }

您可以使用SQL连接映射问题和答案,然后将结果集转换为某种数组。