2011-11-09 6 views
0

データベースから上位3つの行を取得し、それらのフィールドを適切なプロパティクラスオブジェクトに割り当ててフォームに渡しますそれらは表示されました。私は主に、正しいオブジェクトに割り当てるカウント(1,2,3)でトップポジションを伝えるのに問題があります。今は2つのオブジェクトだけをテストしています。フォーム内のオブジェクトにテーブルフィールドを割り当てます。

' 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 

アップデート:私はちょうど他の誰かからこの答えを受け取ったが、私は本当に正確に知っていないすべてのヘルプは素晴らしいことだ、事前ここ

でのおかげで、私が働いている関数のコードです彼らが私に教えようとしていること。何か案は????あなたは、この情報を表示しているフォームで

、あなたが関数を呼び出している方法を検討は:objArt1記事オブジェクトの空のバージョンである

objDB.getTopArticles(1, objArt1) 

機能getTopArticlesは、これらのパラメータを受け付けます。

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

あなたはその後、rankRetrievedによって要求を相殺、あなたが持っているあなたのSQL文を拡張することにより、「正しい」の記事を求めることができます - 1必ずと整列させるためにDB配列の0の点。

3つの異なる記事に対してこれを(フォームから)3回実行します。

+0

これは私が別のフォーラムで受け取ったいくつかの助けですが、私は本当に彼らが私に何かを教えようとしているのか分かりません。 – Jared

答えて

1

私はあなたが行う場合は、すべてのifのための必要性を確認するために失敗します。最初の行は、最高のカウントになります

SELECT * FROM Articles ORDER BY Count DESC LIMIT 3 

、最後の行は、(3の)最低の数を持っています
順番に行をフェッチするだけです。

また、select *は、アンチパターンです。必要な行のみを取得します。

+0

はい、これはまさに私がやりたいことです。私はちょうどそれを行うために正しいSQLまたはVBを知らない。 – Jared

+0

@Salmonerd、 'reader.read()'を呼び出すたびに(正しい順序で)行がフェッチされ、 – Johan

関連する問題