基于计数将表字段分配给表单内的对象

时间:2011-11-09 23:37:38

标签: .net mysql vb.net

我试图根据最高计数从数据库中提取前三行,然后将这些字段分配给相应的属性类对象,然后在显示它们时将其传递给表单。我主要是通过计数(1,2,3)将顶部位置分配给正确的对象。现在我只测试两个对象。任何帮助都会很棒,提前谢谢

以下是我正在处理的函数的代码:

 ' Function to get the top 3 articles by count from the database and assign them to an object that is then passes to the application form
' This is the main function that I have been working with to accomplish passing the articles. As of now it is called after the application form is loaded
Function fncGetArticles() As Boolean
    'Declares needed objects for the function
    Dim myReader1 As MySqlDataReader
    Dim objArticleInfo1 As New clsArticleProperties
    Dim objArticleInfo2 As New clsArticleProperties
    Dim obtain1 As New MySqlCommand


    'Defines connection and SQL statement that selects the top three articles ordered by count
    obtain1.CommandText = "SELECT * FROM Articles ORDER BY Count DESC LIMIT 3"
    obtain1.CommandType = CommandType.Text
    obtain1.Connection = connection
    connection.Open()

    myReader1 = obtain1.ExecuteReader

    While myReader1.Read()

        'these blocks of code assign the field from the database to an object that is then passed to the application form
        'I need a way to tell it to pass the correct fields based off of there count but have been unable to find a way to do this
        'I have experimented a lot with IF...THEN statements for this but have not gotten any thing to work


        'I basically need a better way to assign the fields base off of their order to the designated objects

        'The code after the IF is my way of saying "If the article was 1st in the order then assign it these properties"
        'If myReader1.GetOrdinal("Count") = "SELECT MAX(Count) FROM Articles" Then
        objArticleInfo1.Username = myReader1.GetValue(myReader1.GetOrdinal("Submitby"))
        objArticleInfo1.URL = myReader1.GetValue(myReader1.GetOrdinal("URL"))
        objArticleInfo1.Title = myReader1.GetValue(myReader1.GetOrdinal("Title"))
        objArticleInfo1.SubmitDate = myReader1.GetValue(myReader1.GetOrdinal("Date"))
        objArticleInfo1.Count = myReader1.GetValue(myReader1.GetOrdinal("Count"))
        objArticleInfo1.Comments = myReader1.GetValue(myReader1.GetOrdinal("Comments"))
        objArticleInfo1.Category = myReader1.GetValue(myReader1.GetOrdinal("Category"))
        'End If

        'The code after the IF here is my way of assigning the data from the 2nd spot in the order to the designated object
        'If myReader1.GetOrdinal("Count") < "SELECT MAX(Count) FROM Articles AND Count > SELECT MIN(Count) FROM Articles" Then
        objArticleInfo2.Username = myReader1.GetValue(myReader1.GetOrdinal("Submitby"))
        objArticleInfo2.URL = myReader1.GetValue(myReader1.GetOrdinal("URL"))
        objArticleInfo2.Title = myReader1.GetValue(myReader1.GetOrdinal("Title"))
        objArticleInfo2.SubmitDate = myReader1.GetValue(myReader1.GetOrdinal("Date"))
        objArticleInfo2.Count = myReader1.GetValue(myReader1.GetOrdinal("Count"))
        objArticleInfo2.Comments = myReader1.GetValue(myReader1.GetOrdinal("Comments"))
        objArticleInfo2.Category = myReader1.GetValue(myReader1.GetOrdinal("Category"))
        'End If
        myReader1.Close()

        frmApplicationWindow.passarticles1(objArticleInfo1, objArticleInfo2)

        connection.Close()
    End While
    Return Nothing
End Function

更新:我刚从其他人那里收到了这个答案,但我真的不知道他们想告诉我的确切内容。任何想法????

在显示此信息的表单中,考虑如何调用该函数:

objDB.getTopArticles(1, objArt1)

其中objArt1是文章对象的空版本。

函数getTopArticles接受以下参数:

Function getTopArticles(ByVal rankRetrieved As Integer, ByRef artObject As classArticles)

然后,您可以要求正确的&#39;通过扩展您的SQL语句,通过rankRetrieved - 1来抵消请求,以确保您与DB数组中的0点对齐。

你会为这三篇不同的文章运行三次(从表格中)。

1 个答案:

答案 0 :(得分:1)

我没有看到所有if s的需要如果你这样做:

SELECT * FROM Articles ORDER BY Count DESC LIMIT 3

第一行将是最高计数,最后一行将具有最低计数(三者中)
只需按顺序获取行即可完成。

同样不要做select *,这是一个反模式,只获取你需要的行。