【问题标题】:Records becomes repetitive in vb.net记录在 vb.net 中变得重复
【发布时间】:2023-04-05 21:19:02
【问题描述】:

我有关系数据库。每个学生都有不同的历史(一对多)。数据是正确的,但这里的问题是在datagridview 中加载时学生信息变得重复。我使用DISTINCT 函数但它不起作用。有人可以帮我弄清楚我的代码有什么问题。

加载时的vb.net代码

Using cmd As New SqlClient.SqlCommand("dbo.uspSELECTALL", cn)
    da.SelectCommand = cmd
    dt.Clear()
    da.Fill(dt)
    dgv1.RowTemplate.Height = 50
    dgv1.DataSource = dt
    For i As Integer = 0 To dgv1.Columns.Count - 1
        If TypeOf dgv1.Columns(i) Is DataGridViewImageColumn Then
            DirectCast(dgv1.Columns(i), DataGridViewImageColumn).ImageLayout = DataGridViewImageCellLayout.Stretch
        End If
    Next
End Using

存储过程代码

ALTER PROCEDURE [dbo].[uspSELECTALL]


AS
BEGIN

    SET NOCOUNT ON;

    SELECT  DISTINCT SI.StudentID,SI.Surname,SI.FirstName,SI.MiddleName, SI.StudAddress , 
            SI.BirthDay,SI.Gender, SI.Nationality, SI.BirthPlace,
            SI.TelNum,SI.SchoolWhereGraduated , 
            SI.DatesWhenGraduated, SI.SchoolLastAttended,
            SI.Note,SI.StudImage,
            PI.Father_FirstName,PI.Father_LastName,
            PI.Father_MI,PI.Father_Occupation, 
            PI.Father_TelNUm, PI.Mother_FirstName, PI.Mother_LastName,
            PI.Mother_MI,PI.Mother_Occupation,PI.Mother_TelNum,
            PI.Contact_FirstName,PI.Contact_LastName,PI.Contact_MI,
            PI.Contact_Mobile,PI.Contact_TelNum,PI.Contact_Address,
            SH.SchoolYear,SH.Levels,SH.Section,SH.DateEnrolled

            FROM StudentInformation SI 
            JOIN StudentHistory SH  
                ON SI.StudentID = SH.StudentID
            JOIN ParentInformation PI
                ON PI.ParentID = SI.ParentID

END

【问题讨论】:

  • 您是否有多个父母信息行?如果您直接对数据库运行此 sql,您会看到重复项吗?
  • 运行到sql server时,数据是正确的并显示这个SQL SERVER,但是当我加载到datagridview时,它有一个重复的记录。但是,仅当一个学生在 studentHistory 表中有很多历史记录时才会发生此问题
  • dt 在将其加载到 datagridview 之前是否具有预期的行数?
  • 你的意思是,你想输出多个不同的历史,只有一个不重复的学生 ID?
  • @Cyval 是的,这就是我想要做的。我只希望在加载时不重复的学生 ID 在 GridView 中显示。

标签: sql-server vb.net sql-server-2008 datagridview


【解决方案1】:

如果您想在DataGridView 中显示仅区分学生的 id,
那么您需要使用另一个查询。
如果学生有多个历史记录,您当前的查询总是为每个学生返回多行。 一种方法是从SELECT 语句中删除表StudentHistory 的所有列,并在查询中保留DISTINCTkeyword。

SELECT  DISTINCT SI.StudentID,SI.Surname,SI.FirstName,SI.MiddleName, SI.StudAddress , 
        SI.BirthDay,SI.Gender, SI.Nationality, SI.BirthPlace,
        SI.TelNum,SI.SchoolWhereGraduated , 
        SI.DatesWhenGraduated, SI.SchoolLastAttended,
        SI.Note,SI.StudImage,
        PI.Father_FirstName,PI.Father_LastName,
        PI.Father_MI,PI.Father_Occupation, 
        PI.Father_TelNUm, PI.Mother_FirstName, PI.Mother_LastName,
        PI.Mother_MI,PI.Mother_Occupation,PI.Mother_TelNum,
        PI.Contact_FirstName,PI.Contact_LastName,PI.Contact_MI,
        PI.Contact_Mobile,PI.Contact_TelNum,PI.Contact_Address

FROM StudentInformation SI 
        JOIN StudentHistory SH  
            ON SI.StudentID = SH.StudentID
        JOIN ParentInformation PI
            ON PI.ParentID = SI.ParentID

另一种方法是过滤由您的查询填充的DataTable

Using cmd As New SqlClient.SqlCommand("dbo.uspSELECTALL", cn)
    da.SelectCommand = cmd
    dt.Clear()
    da.Fill(dt)
    dgv1.RowTemplate.Height = 50
    'Filtering for distinct rows
    Dim view As New DataView(GetData())
    Dim distinctColumnNames As String() =
    {
        "StudentID", "Surname", "FirstName", "MiddleName",
        "StudAddress" ' and so on
    }
    Dim distinctValues As DataTable = view.ToTable(True, distinctColumnNames)       
    dgv1.DataSource = distinctValues 
    'Your other code
End Using

view.ToTable(True, distinctColumnNames) 如果第一个参数是 True,则仅包含基于列名的不同行

【讨论】:

  • 感谢您的评论。你能给我看一些示例代码吗?我只是开发vb的初学者,有点困惑。
  • 感谢您的大力帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 2021-11-25
相关资源
最近更新 更多