【问题标题】:Assigning table fields to objects within form based off of count根据计数将表格字段分配给表单中的对象
【发布时间】:2011-11-09 23:37:38
【问题描述】:

我正在尝试根据最高计数从数据库中提取前三行,然后将这些字段分配给适当的属性类对象,然后我将其传递给显示它们的表单。我主要无法通过计数(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)

然后,您可以通过扩展您拥有的 SQL 语句来请求“正确”文章,将请求偏移 rankRetrieved – 1 以确保您与 DB 数组中的 0 点对齐。

对于三篇不同的文章,您将运行三次(从表单中)。

【问题讨论】:

  • 这是我在另一个论坛上收到的一些帮助,但我真的不知道他们想告诉我什么,有什么想法吗???

标签: .net mysql vb.net


【解决方案1】:

我看不出需要所有ifs 如果你这样做:

SELECT * FROM Articles ORDER BY Count DESC LIMIT 3

第一行的计数最高,最后一行的计数最低(三者中)
只需按顺序获取行即可。

也不要这样做select *,这是一种反模式,只获取您需要的行。

【讨论】:

  • 是的,这正是我想做的。我只是不知道正确的 SQL 或 VB 来完成它。
  • @Salmonerd,每次调用reader.read() 都会获取一行(按正确的顺序)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2014-09-12
  • 1970-01-01
  • 1970-01-01
  • 2015-11-05
相关资源
最近更新 更多