我的VB.NET循环代码不起作用。有任何想法吗?

时间:2019-06-27 20:25:18

标签: vb.net

我正在编写一个脚本来帮助我填写MySQL表。它基本上遍历每年的组并设置并为其主题开设课程。例如:Year Group 8,Set 4,英语为84En等,我这样做了,因此它遍历ArrayList并更改了一些变量,但我似乎无法使其正常工作。

我真的不知道该怎么诚实,因为我已经检查了一下,也不知道自己是否盲目,但是我看不到任何错误。

Dim subjects As New ArrayList
subjects.Add("English")
subjects.Add("Welsh")
subjects.Add("French")
subjects.Add("Maths")
subjects.Add("Science")
subjects.Add("History")
subjects.Add("Geography")
subjects.Add("Pe")
subjects.Add("Re")
subjects.Add("Tutorial")
subjects.Add("Music")
subjects.Add("Drama")
subjects.Add("Technology")
subjects.Add("It")
subjects.Add("Art")
Dim upsubjects As New ArrayList
upsubjects.Add("English")
upsubjects.Add("Welsh")
upsubjects.Add("Physics")
upsubjects.Add("Biology")
upsubjects.Add("Chemistry")
upsubjects.Add("French")
upsubjects.Add("Maths")
upsubjects.Add("Science")
upsubjects.Add("History")
upsubjects.Add("Music")
upsubjects.Add("Technology")
upsubjects.Add("It")
Dim year As Integer = 7
Dim setting As Integer = 8
Dim done As Boolean = False
Dim cmd As MySqlCommand
While done = False
    For Each subject As String In subjects
        If year = 9 And setting = 8 Then
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = 1
        End If
        If setting = 8 And Not year = 9 Then
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = 1
            year = year + 1
        Else
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = setting + 1
        End If

    Next
    For Each subject As String In upsubjects
        If year = 11 And setting = 8 Then
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = 1
            done = True
        End If
        If setting = 8 And Not year = 11 Then
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = 1
            year = year + 1
        Else
            cmd = New MySqlCommand("INSERT INTO classes (`classid`, `class`) VALUES ('" + year + setting + subject.Substring(2, (subject.Length - 2)).ToUpper + "', '" + subject.Substring(2, (subject.Length - 2)) + "')", conn)
            cmd.ExecuteNonQuery()
            setting = setting + 1
        End If

    Next
End While

我没有收到任何错误消息,仅加载了表单(应该执行代码时)并且什么也不做。刷新了我的MySQL数据库,什么也没有。关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)

如果要将数值settingyear连接到查询字符串中,是否不需要像CStr()那样将它们转换为字符串?

未收到错误提示我该代码未在执行,但是正如其他人在评论中所述,您可以立即添加一个“停止”来进行检查。

此外,请阅读“数据库规范化”,有关将各种值组合到表中的单个列中,请考虑您将如何检索该信息以及如果保留该信息将有多容易它分开。