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

时间:2017-01-31 11:35:48

标签: sql vb.net ms-access oledb vb.net-2010

我正在编写一个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

我的问题是,我需要能够选择动态数量的SubjectID和GradeID,以防学生摄入的科目多于或少于正常科目。

我的查询产生:

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

CurrentID是" 1"出于此目的。

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

我需要能够将这三个成绩ID中的每一个都放在一个单独的值中,可以使用数组,但我不知道如何对其进行编码。我之前尝试过它,如#34; YourGrade(4),YourSubject(4)"。

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

2 个答案:

答案 0 :(得分:0)

为"学生"创建域对象然后将记录加载到学生对象列表中。

我不了解VB.net,但C#中的等效域对象看起来像这样:

@RequestMapping(method = { RequestMethod.GET }, value = { "/docente_cv" })
public ResponseEntity<InputStreamResource> downloadCsv(@PathVariable("idForm") String idForm)
{
    Connection con = null;
    String fileName = "";
    try
    {
        con = DBCon.establishConnection();
            String sql = "SELECT cv,content_type,filename FROM Docenti_pj where id_docente=? ";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setLong( 1 , idDocente);
            ResultSet rs = ps.executeQuery();
            while( rs.next() ) 
        {
            Blob blob = rs.getBlob("cv"); //prende il blob del curriculum
                    String contentType = rs.getString("content_Type");
                    fileName = rs.getString("filename");
            if(blob!=null)
            {
                            InputStream inputStream = blob.getBinaryStream();
                response.setHeader("Content-Disposition", "inline;filename=\"" +fileName+ "\"");
                HttpHeaders respHeaders = new HttpHeaders();
                MediaType mediaType = MediaType.parseMediaType(contentType)
                    respHeaders.setContentType(mediaType);
                    respHeaders.setContentLength(file.length());
                    respHeaders.setContentDispositionFormData("attachment", fileName);
                    InputStreamResource isr = new InputStreamResource(inputStream);
                    return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
            }
            else
            {
                return new ResponseEntity<InputStreamResource>(HttpStatus.NO_CONTENT);
            }
        }
    }
    catch (Exception e)
    {
        String message = "Errore nel download del file "+fileName+"; "+e.getMessage();
        logger.error(message, e);
        return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
    finally
    {
      if(con != null)
        con.close();
    }
}

然后循环访问dataReader并填充列表的代码:

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

- 编辑2/2/2017 -

原来网上有免费的转换器。这些是上面代码片段的VB.net等价物。

类别:

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"])
    });
}

数据库代码:

昏暗的结果作为新名单(学生)()

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

答案 1 :(得分:0)

我做了一些挖掘,发现以我试图的方式填写数据网格,最简单的方法是使用INNER JOIN 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;

那是使用我的表名,例如。