0
我想從數據庫中填充兩個ArrayLists(listOfAnswers和listOfAnswerIDs)字段('answer'和'answer_id')。使用MySQL數據填充ArrayList
當字符串cmdText中的question_id = 1時,下面的代碼似乎很好地工作。但是,如果我將其更改爲2或3,則ArrayLists保持空白,我不知道爲什麼。有任何想法嗎?
我認爲這是與字符串作爲cmdGetAnswersQuery 「SELECT * FROM答案WHERE question_id = 2」 的作品做...
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
MySqlDataReader reader;
ArrayList listOfAnswerIDs = new ArrayList();
ArrayList listOfAnswers = new ArrayList();
try
{
conn.Open();
string cmdText = "SELECT * FROM questions_t WHERE question_id=2";
MySqlCommand cmd = new MySqlCommand(cmdText, conn);
cmd.Parameters.Add("@ModuleID", MySqlDbType.Int32);
cmd.Parameters["@ModuleID"].Value = ddlModules.SelectedValue;
reader = cmd.ExecuteReader();
if (reader.Read())
{
lblQuestion.Text = reader["question"].ToString();
ViewState["QuestionID"] = reader["question_id"].ToString();
ViewState["AnswerID"] = reader["correct_answer_id"].ToString();
reader.Close();
string cmdGetAnswersQuery = "SELECT * FROM answers WHERE [email protected]";
MySqlCommand cmdGetAnswers = new MySqlCommand(cmdGetAnswersQuery, conn);
cmdGetAnswers.Parameters.Add("@QuestionID", MySqlDbType.Int32);
cmdGetAnswers.Parameters["@QuestionID"].Value = ViewState["AnswerID"];
reader = cmdGetAnswers.ExecuteReader();
while (reader.Read())
{
listOfAnswerIDs.Add(reader["answer_id"].ToString());
listOfAnswers.Add(reader["answer"].ToString());
}
reader.Close();
populateAnswers(listOfAnswers, listOfAnswerIDs);
}
else
{
reader.Close();
lblError.Text = "(no questions found)";
}
}
catch
{
lblError.Text = "Database connection error - failed to insert record.";
}
finally
{
conn.Close();
}
實際的數據庫內容如下所示: http://i.imgur.com/3S4lV60.png http://i.imgur.com/8A913xF.png
嗯,我討厭問這個問題,但是......甚至有'question_id = 2'或'question_id = 3'的行嗎? – Jashaszun
@Jashaszun是啊 - http://imgur.com/8A913xF,http://imgur.com/3S4lV60 – Bhav
年2k14,我們仍然使用'ArrayList'編碼? –