代码之家  ›  专栏  ›  技术社区  ›  Will Swindell

(SQL,VB.NET)如何从一行中选择多个值并将其分配给变量?

  •  0
  • Will Swindell  · 技术社区  · 8 年前

    我正在编写一个SELECT查询以用于我的项目。到目前为止,我已经

    Dim conn As New OleDbConnection
        Dim StudentID, GradeID, SubjectID As Integer
        Dim YourGrade(4), YourSubject(4) As String
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =H:\Year 13 Computer Science\Project\Usernames and Passwords.accdb"
    
    
            conn.Open()
            Dim sql = "Select * From Grades where StudentID =" & CurrentID
            Dim cmd As New OleDbCommand(sql, conn)
            Dim dr As OleDbDataReader = cmd.ExecuteReader
    
            While dr.Read
                StudentID = dr("StudentID")
                GradeID = dr("GradeID")
                SubjectID = dr("SubjectID")
            End While
    

    我的问题是,我需要能够选择动态数量的科目和年级,以防学生选修的科目多于或少于正常三个科目。

    我的查询生成:

    StudentID   GradeID SubjectID
    1              2        1
    1              4        13
    1              3        19
    

    为此,当前ID为“1”。

    每个GradeID和SubjectID对应于其他表中的值,我可以稍后处理这些表。

    我需要能够将这三个等级ID中的每一个都放在一个单独的值中,可以使用数组,但我不知道如何编码。我之前试过了,如“你的等级(4),你的科目(4)”所示。

    我打算使用这些数据来填充数据网格。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Hambone    8 年前

    为“Student”创建域对象,然后将记录加载到Student对象列表中。

    我不知道VB.net,但C#中的等效域对象如下所示:

    public class Student
    {
        public int StudentId { get; set; }
        public int GradeId { get; set; }
        public int SubjectId { get; set; }
    }
    

    然后代码循环通过dataReader并填充列表:

    List<Student> results = new List<Student>();
    
    while (dr.Read())
    {
        results.Add(new Student()
        {
             StudentId = Convert.ToInt32(dr["StudentID"]),
             GradeId = Convert.ToInt32(dr["GradeId"]),
             SubjectId = Convert.ToInt32(dr["SubjectId"])
        });
    }
    

    --2017年2月2日编辑--

    事实证明,网络上有免费的转换器。这些是VB.net与上述代码段的等价物。

    类别:

    Public Class Student
        Public Property StudentId() As Integer
            Get
                Return m_StudentId
            End Get
            Set
                m_StudentId = Value
            End Set
        End Property
        Private m_StudentId As Integer
        Public Property GradeId() As Integer
            Get
                Return m_GradeId
            End Get
            Set
                m_GradeId = Value
            End Set
        End Property
        Private m_GradeId As Integer
        Public Property SubjectId() As Integer
            Get
                Return m_SubjectId
            End Get
            Set
                m_SubjectId = Value
            End Set
        End Property
        Private m_SubjectId As Integer
    End Class
    

    数据库代码:

    Dim结果作为新列表(学生)()

    While dr.Read()
        results.Add(New Student() With { _
            Key .StudentId = Convert.ToInt32(dr("StudentID")), _
            Key .GradeId = Convert.ToInt32(dr("GradeId")), _
            Key .SubjectId = Convert.ToInt32(dr("SubjectId")) _
        })
    End While
    
        2
  •  0
  •   Will Swindell    8 年前

    我做了一些挖掘,发现要以我尝试的方式填充数据网格,最简单的方法是使用内部联接SQL语句。在SQL中,查询为:

        SELECT Students.FirstName, Students.LastName, Subjects.SubjectName,    GradeVals.Grade
    FROM GradeVals INNER JOIN (Students INNER JOIN (Subjects INNER JOIN Grades ON Subjects.SubjectID = Grades.SubjectID) ON Students.StudentID = Grades.StudentID) ON GradeVals.GradeID = Grades.GradeID;
    

    这使用了我的表名,比如。