vb.net sqlite如何遍历所选记录并将每个记录作为参数传递给另一个函数

时间:2010-06-05 05:57:59

标签: vb.net sqlite loops records

我有一个包含以下字段的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

非常感谢你的帮助。

3 个答案:

答案 0 :(得分:1)

数据表有一个DataRow对象,您可以将其传递给函数。

答案 1 :(得分: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

请注意,这仍然是伪代码,因为这不会编译:)

答案 2 :(得分:0)

我建议你将这个逻辑移到数据库中。 SQL可以执行这些检查并通过带有where子句的update语句更新数据。