【发布时间】:2014-09-26 04:24:17
【问题描述】:
想要将数组列表中的值循环到列表框中。目前,只有 arraylist 中的最新项被添加到列表框中。
按钮下的代码
string strRating = txtRatingComment.Text;
string strReturnedFilePath = db.retrievePictureRating(strRating);
//clear album
listBoxSearchResult.Items.Clear();
//Add filepaths into arraylist
pictureList.Add(strReturnedFilePath);
//Loop arraylist into listbox
for (int i = 0; i < pictureList.Count; i++)
{
listBoxSearchResult.Items.Add(pictureList[i]);
}
Database类下的代码
public string retrievePictureRating(string strRating)
{
string query = "SELECT pictureFilePath FROM picture where pictureRating = @pictureRating;";
string pictureFilePath = "";
//open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.Parameters.AddWithValue("@pictureRating", strRating);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
pictureFilePath = dataReader["pictureFilePath"].ToString();
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
}
return pictureFilePath;
}
【问题讨论】:
标签: c# database arraylist listbox