2010-06-05 37 views
0

我有以下字段的SQLite表:vb.net sqlite的如何通過選擇循環記錄和傳遞每一個記錄作爲參數傳遞給另一個函數

Langauge  level  hours 
German  2   50 
French  3   40 
English 1   60 
German  1   10 
English 2   50 
English 3   60 
German  1   20 
French  2   40 

我想通過記錄迴路基於語言和其他條件,然後將當前選定的記錄傳遞給不同的功能。 所以我有以下實際代碼和psudo代碼的混合。我需要將psudo代碼轉換爲實際代碼的幫助。我發現很難這樣做。

以下是我有:

Private sub mainp() 
    Dim oslcConnection As New SQLite.SQLiteConnection 
    Dim oslcCommand As SQLite.SQLiteCommand 
    Dim langs() As String = {"German", "French", "English"} 
    Dim i as Integer = 0 
    oslcConnection.ConnectionString = "Data Source=" & My.Settings.dbFullPath & ";" 
    oslcConnection.Open() 
    oslcCommand = oslcConnection.CreateCommand 
    Do While i <= langs.count 
    If langs(i) = "German" Then 
     oslcCommand.CommandText = "SELECT * FROM table WHERE language = '" & langs(i) & "';" 
     For each record selected    'psudo code 
     If level = 1 Then     'psudo code 
      update level to 2    'psudo code 
      minorp(currentRecord)   'psudo code: calling minorp function and passing the whole record as a parameter 
     End If       'psudo code 
     If level = 2 Then     'psudo code 
      update level to 3    'psudo code 
      minorp(currentRecord)   'psudo code: calling minorp function and passing the whole record as a parameter 
     End If       'psudo code 
     Next         'psudo code 
    End If 

    If langs(i) = "French" Then 
     oslcCommand.CommandText = "SELECT * FROM table WHERE language = '" & langs(i) & "';" 
     For each record selected    'psudo code 
     If level = 1 Then     'psudo code 
      update level to 2    'psudo code 
      minorp(currentRecord)   'psudo code: calling minorp function and passing the whole record as a parameter 
     End If       'psudo code 
     If level = 2 Then     'psudo code 
      update level to 3    'psudo code 
      minorp(currentRecord)   'psudo code: calling minorp function and passing the whole record as a parameter 
     End If       'psudo code 
     Next         'psudo code 
    End If 
Loop 
End Sub 

你的幫助非常感謝。

回答

1

數據表有一個DataRow對象,可以傳遞給你的函數。

+0

你能告訴我如何用sqlite做到這一點。我在使用vb.net和sqlite查找示例時遇到了一些困難。謝謝 – mazrabul 2010-06-06 10:04:48

+0

既然你顯示僞代碼,我假設你要麼從你的查詢中獲取DataSet或DataReader。檢查DataTable和DataReader類,它們都具有DataRows集合(用於DataReader的行?)。 – Raj 2010-06-06 10:27:51

+0

是的,我正在使用數據集。檢查了課程,發現我在尋找什麼。謝謝 – mazrabul 2010-06-06 12:11:19

0

我建議你創建一個類,例如

public class LanguageCourse 

    'Prob better to make these properties 
    public Language as string 
    public Level as integer 
    public Hours as integer 

    public sub new(language as string, level as integer, hours as integer) 
    Language = language 
    Level = level 
    Hours = hours 
    end sub 

end class 

上面你的代碼可能成爲以下:

Private sub mainp() 
    Dim oslcConnection As New SQLite.SQLiteConnection 
    Dim oslcCommand As SQLite.SQLiteCommand 
    Dim langs() As String = {"German", "French", "English"} 
    Dim i as Integer = 0 
    oslcConnection.ConnectionString = "Data Source=" & My.Settings.dbFullPath & ";" 
    oslcConnection.Open() 
    oslcCommand = oslcConnection.CreateCommand 

    'Not sure why you were looping round these like this. It's also not a great idea to 
    'build up your sql queries by concactenating strings, better to parameteris them, but 
    'seeing as how this seems to be hard coded anyway, better even like this: 

     dim course as LanguageCourse 

     oslcCommand.CommandText = "SELECT * FROM table WHERE language IN ("German", "French", "English");" 
     For each record selected    'psudo code 

     course = new LanguageCourse(record.language, record.level, record.hours) 

     'This function should handle your update as you just seem to be adding one to 
     'something, for certain criteria. 
     minorp(course) 

     Next         'psudo code 


End Sub 

注意,這仍然是僞代碼,因爲這將不能編譯:)

0

我會建議你提出這個邏輯數據庫。 SQL可以通過帶有where子句的更新語句執行這些檢查並更新數據。

相關問題