如何根据数据库中的数据编写条件语句

时间:2018-12-04 06:00:13

标签: database vb.net

我是vb的新手。我创建了2个表单(Form1和Form2)

Form1是用户的配置文件主题,将保存在数据库中。有三个组合框=名称,主题1,主题2。

form1屏幕截图:

Screen Shot

在数据库中查看:将数据保存到数据库中没有任何问题。我使用了下面的代码。

数据库屏幕截图:

Screen Shot

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim constr As String = "server=localhost;user=root;database=test;port=3306;password=root123;SslMode=none"
    Dim conn As MySqlConnection = New MySqlConnection(constr)
    Dim result As Integer
    'If True Then
    Try
        conn.Open()
        With {}
            Dim cmd As MySqlCommand

            cmd = New MySqlCommand("INSERT INTO profilesubject(name,subject1,subject2) VALUES(@name,@subject1,@subject2)", conn)
            cmd.Parameters.AddWithValue("@name", ComboBox1.Text)

            cmd.Parameters.AddWithValue("@subject1", ComboBox2.Text)
            cmd.Parameters.AddWithValue("@subject2", ComboBox3.Text)
            result = cmd.ExecuteNonQuery()
            'conn.Close()
        End With
        'End If

        If result > 0 Then

            MsgBox("Record has been saved")

        Else
            MsgBox("Error!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        End If

    Catch ex As Exception
        Console.WriteLine(ex.ToString())
        MsgBox(ex.Message)

    Finally
        conn.Close()
    End Try

End Sub

例如:

用户1(姓名:John,主题1:数学,主题2:英语)

用户2(名称:Mark,主题1:数学,主题2:英语)

用户3(名称:Brenda,主题1:历史,主题2:科学)

保存数据后,当我们单击form1中的按钮时,将出现form2。

form2屏幕截图:

Screen Shot

form2有2个组合框,分别是主题和名称。根据从先前数据库中保存的数据,如果用户在form2中为主题选择“数学”,则选择“数学”的名称将出现在名称的组合框中(从数据库中选择)。因此,应该显示组合框值(John和Mark)。如果用户为主题选择“历史记录”,则名称组合框应显示为“ Brenda”。

问题是,我不知道如何从数据库中编写适当的条件语句。这是我的代码。但它不起作用。我希望你们能帮助我解决问题。

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim connStr As String = "server=localhost;user=root;database=test;port=3306;password=root123;SslMode=none"
    Dim con As New MySqlConnection(connStr)
    Dim cmd As New MySqlCommand()

    Dim reader As MySqlDataReader
    con.Open()
    cmd.Connection = con
    cmd.CommandText = "select subject1,subject2 FROM profilesubject WHERE subject1=@subject1,subject2=@subject2)"
    reader = cmd.ExecuteReader
    cmd.Parameters("@subject1").Value = ComboBox1.Text
    cmd.Parameters("@subject2").Value = ComboBox1.Text

    If (reader.Item("@subject1").Equals(ComboBox1.Text) & (reader.Item("@subject2").Equals(ComboBox1.Text))) Then

        Dim adapter As New MySqlDataAdapter("SELECT `id`, `name`, `subject1`, `subject2` FROM test.profilesubject", con)
        Dim table As New DataTable()

        adapter.Fill(table)

        ComboBox1.DataSource = table

        ComboBox1.ValueMember = "id"
        ComboBox1.DisplayMember = "name"
    End If
    con.Close()
End Sub

1 个答案:

答案 0 :(得分:0)

如果我理解正确,Form1.Subject1,Form1.Subject2和Form2.Subject组合框都具有相同的主题列表。因此,您需要在Form2的组合名称中列出所有在Form2中选择了该主题的学生的列表。主题为Subject1或Subject2。

要实现此目的,您需要搜索2列主题。您需要返回ID和名称。

从test.profilesubject中选择名称,id,其中subject1 = @subject或subject2 = @subject;

A Using ... End Using块将关闭并处置可能正在使用非托管代码的数据库对象。即使有错误,也会发生这种情况。

使用参数做好!这对于保护数据库非常重要。

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
        'Use this event instead of SelectedIndexChanged so it won't jump
        'around when you load the combobox
        Dim connStr As String = "server=localhost;user=root;database=test;port=3306;password=root123;SslMode=none"
        Dim table As New DataTable()
        Try
            Using con As New MySqlConnection(connStr)
                'Pass the sql statement and the connection to
                'the Constructor of the Command
                Using cmd As New MySqlCommand("Select name, id From test.profilesubject Where subject1 = @subject Or subject2 = @subject;", con)
                    'you are looking for the same value in subject1 and subject2
                    'so you only need one parameter
                    'Check the data type of the subject field in your database
                    'and alter this line if necessary
                    cmd.Parameters.Add("@subject", MySqlDbType.Text).Value = ComboBox1.Text
                    con.Open()
                    'the Load method of a DataTable takes a DataReader as
                    'a parameter and ExecuteReader returns a DataReader
                    table.Load(cmd.ExecuteReader)
                End Using
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        If table.Rows.Count > 0 Then
            ComboBox1.DataSource = table
            ComboBox1.ValueMember = "id"
            ComboBox1.DisplayMember = "name"
        Else
            MessageBox.Show("Sorry, no matches")
        End If
End Sub