如何将数据从数据库加载到列表框

时间:2018-11-14 08:54:29

标签: c# listbox sqlconnection

我的程序可以加载列表框标题,但不能加载整个表中的实际数据。

(我如何连接数据库):

const string connectionString = "Data Source=test;Initial Catalog=dbi391731;User ID=test;Password=test";
        SqlConnection conn = new SqlConnection(connectionString);

我正在使用一个类来加载数据:

public List<ScoreMdw> GetScoreMdwList()
        {
            List<ScoreMdw> scoremdwList = new List<ScoreMdw>();
            conn.Open();
            string query = ("Select employeeid, questionid, score from contentment");
            SqlCommand cmd = new SqlCommand(query, conn);

            try
            {
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        ScoreMdw sm = new ScoreMdw((int)dr["employeeid"], (int)dr["questionid"], (char)dr["score"]);
                        scoremdwList.Add(sm);
                    }
                }

            }
            catch (Exception ex)
            {
                Exception error = new Exception("error", ex);
                throw error;
            }

            finally
            {
                conn.Close();
            }

            return scoremdwList;
        }

在while循环中,我正在使用其他类:

class ScoreMdw
    {
        private int employeeid;
        private int questionid;
        private char score;

        public ScoreMdw(int nr, int id, char s)
        {
            this.employeeid= nr;
            this.questionid= id;
            this.score = s;
        }

        public int EmployeeId
        {
            get { return employeeid; }
        }

        public int QuestionId
        {
            get { return questionid; }
        }

        public char Score
        {
            get { return score; }
        }

        public override string ToString()
        {
            string s = string.Format("{0} \t{1} \t{2}", this.employeeid, this.questionid, this.score);
            return s;
        }
    }

在我的主窗口中,我正在这样做:

 private void btnLoadScores_Click(object sender, RoutedEventArgs e)
        {
            scoremdwList = new List<ScoreMdw>();

            try
            {
                conn.Open();

                List<string> headers = so.GetContentmentHeaders();

                foreach (string header in headers)
                    txtHeader.Text += header + "\t";

                scoremdwList = so.GetScoreMdwList();
                lbScores.ItemsSource = scoremdwList;
            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                conn.Close();
            }
        }

我得到了我在课堂上犯的错误(“错误”)。我不知道我在做什么错?也许与连接有关?我是否以错误的方式打开和关闭它?

1 个答案:

答案 0 :(得分:0)

我可以请您向我们展示ex.Message吗?因此我们知道可能的错误是什么。

try
{
   using(SqlDataReader dr = cmd.ExecuteReader())
   {
       while(dr.read())
       {
          ScoreMdw sm = new ScoreMdw((int)dr["employeeid"], (int)dr["questionid"], (char)dr["score"]);
          scoremdwList.Add(sm);
       }
   }
}
catch(Exception ex)
{
   System.Diagnostics.Debug.WriteLine(ex.Message); // <= here you will get you errormessage that is important to fix your error.
   Exception error = new Exception("error", ex);
   throw error;
}