示例#1
0
        static BallotPaper SelectVote(Contest contest, int ballotPaperId)
        {
            string sql = "Select CandidateId, VotePreference from Votes where BallotPaperId = @BallotPaperId ";

            try
            {
                BallotPaper ballotPaper = new BallotPaper();

                using (SqlConnection cnn = new SqlConnection(ConnectionString))
                {
                    cnn.Open();
                    using (SqlCommand cmd = new SqlCommand(sql, cnn))
                    {
                        cmd.Parameters.Add("@BallotPaperId", SqlDbType.Int).Value = ballotPaperId;

                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read() != false)
                            {
                                int candidateId    = (int)reader["CandidateId"];
                                int votePreference = (int)reader["VotePreference"];

                                Candidate c = contest.GetCandidateById(candidateId);

                                Vote v = new Vote(c, votePreference);
                                ballotPaper.AddVote(v);
                                rowCount++;
                            }
                        }
                    }
                }
                return(ballotPaper);
            }
            catch (Exception ee)
            {
                MessageBox.Show("err: " + ee.Message);
            }
            return(null);
        }