我有关系数据库。每个学生都有不同的历史(一对多)。数据是正确的,但问题是学生信息在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
答案 0 :(得分:0)
如果您希望 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setCancelable(false);
中的节目仅区分学生的身份,则
然后你需要使用另一个查询
如果学生有多个历史记录,则您当前的查询总是返回每个学生的多行。
一种方法是从DataGridView
语句中删除表StudentHistory
的所有列,并在查询中保留SELECT
个关键字。
DISTINCT
另一种方法是过滤您的查询填充的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)
,那么将仅包含基于列名的不同行