在datagrid中显示两个相同的数据

时间:2013-06-20 07:54:20

标签: c# winforms sql-server-2008-r2

[求助],gzaxx的答案。

即使在数据库中没有插入成员两次,在数据网格中也会显示两次! 我有这个表格,我添加成员,另一个表格,我添加成员的付款。当我向成员添加超过1个付款时,该成员在网格中显示两次。

列出我正在使用的成员:

        public List<Member> ListMembers()
        {
            List<Member> Members = new List<Member>();


            string STATUS = "SELECT m.Id, m.Name,m.Surname, m.EntryDate, p.EndDay FROM Members m left join Payments p on m.Id = p.MemberId order by m.Id ";
            using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(STATUS, sqlConnection))
                {
                    var sqlReader = sqlCommand.ExecuteReader();

                    while (sqlReader.Read())
                    {
                        var member = new Member
                        {
                            Id = Convert.ToInt32(sqlReader["Id"]),
                            Name = sqlReader["Name"].ToString(),
                            Surname = sqlReader["Surname"].ToString(),
                            EntryDate = Convert.ToDateTime(sqlReader["EntryDate"])
                        };

                        if (sqlReader["EndDay"].ToString() != "")
                        if (Convert.ToDateTime(sqlReader["EndDay"]) < DateTime.Today)
                        {
                            member.Status = Status.Unpaid.ToString();
                        }

                        else
                        {
                            member.Status = Status.Paid.ToString();
                        }

                        Members.Add(member);
                    }
                }
            }

            return Members;
        }

付款:

        public List<Payment> ListPayments(Payment entity)
        {
            List<Payment> Payments = new List<Payment>();

            string SELECT = "SELECT * FROM Payments WHERE MemberId = @MemberId";

            using (sqlConnection = new SqlConnection(sqlConnectionString_WORK))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand(SELECT, sqlConnection))
                {
                    sqlCommand.Parameters.Add("@MemberId", SqlDbType.Int).Value = entity.MemberId;

                    var sqlReader = sqlCommand.ExecuteReader();

                    while (sqlReader.Read())
                    {
                        var payment = new Payment
                        {
                            Id = Convert.ToInt32(sqlReader["Id"]),
                            MemberId = Convert.ToInt32(sqlReader["MemberId"]),
                            Amount = Convert.ToDecimal(sqlReader["Amount"]),
                            StartDay = Convert.ToDateTime(sqlReader["StartDay"]),
                            EndDay = Convert.ToDateTime(sqlReader["EndDay"])
                        };

                        Payments.Add(payment);
                    }
                }
            }

            return Payments;
        }

如果会员没有付款,则会向其状态列显示NOTHING。如果会员有一笔付款,它将显示其状态。但是,如果我添加一个新的付款(超过一个),数据网格将显示相同的成员等于其在网格上的付款。我做错了什么???

2 个答案:

答案 0 :(得分:1)

此查询:

SELECT m.Id, m.Name,m.Surname, m.EntryDate, p.EndDay FROM Members m left join Payments p on m.Id = p.MemberId order by m.Id 

将返回一个成员(如果找到)与给定成员匹配的所有Payment.EndDay条记录。因此,如果您只想要一行(也许是最新的付款),那么您的查询应该如下所示:

SELECT m.Id, m.Name,m.Surname, m.EntryDate, MAX(p.EndDay) as EndDay
FROM Members m left join Payments p on m.Id = p.MemberId
GROUP BY m.Id, m.Name,m.Surname, m.EntryDate
order by m.Id

答案 1 :(得分:0)

这是左连接的工作原理。 http://www.w3schools.com/sql/sql_join_left.asp

加倍的值是由“加入”引起的 - 返回的记录与付款一样多。将它们分组,正如@gzaxx建议的那样可能是一个好主意

如果没有付款的列中有空,取决于您正在使用的数据库,您可以在查询中使用ISNULL(mssql)(http://www.w3schools.com/sql/sql_isnull.asp)命令对其进行补救。