首页 > 解决方案 > 如何根据数据库中的数据编写条件语句

问题描述

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

Form1 是用户的个人资料主题,将保存在数据库中。有三个组合框 = 名称、主题 1、主题 2。

form1 屏幕截图:

截屏

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

数据库屏幕截图:

截屏

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 屏幕截图:

截屏

form2 有 2 个组合框,分别是主题和名称。根据以前数据库保存的数据,如果用户在form2中选择“数学”作为主题,选择“数学”的名称将出现在名称的组合框中(从数据库中选择)。所以组合框的值应该出现(约翰和马克),如果用户选择“历史”作为主题,名称的组合框应该出现“布伦达”。

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

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

标签: databasevb.net

解决方案


如果我理解正确的话,Form1.Subject1、Form1.Subject2 和 Form2.Subject 组合框都有相同的主题列表。因此,您希望在 Form2 中以组合命名所有在 Form2.Subject 中选择主题的学生的列表,将其命名为 Subject1 或 Subject2。

为此,您需要搜索 2 列的主题。您需要返回的 id 和 name。

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

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

推荐阅读