用vb.net和组合框显示数据库中的数据

col17t5w  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(391)

我的语法中有一个错误,它表示 AND 操作员

If ComboBox1.Text = "" And ComboBox2.Text = "" Then
    MsgBox("Please Fill In The Details")
Else
    Try
        con.Open()
        Select Case ComboBox1.Text
            Case "Students Info"
                Dim a = "SELECT * FROM studentdb1 WHERE class = " & ComboBox2.SelectedText & " AND WHERE date_registered BETWEEN " & date1.Text & " AND " & date2.Text & ""
                displayTable(a)
        End Select

        con.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End If

我创建了一个名为 displayTable 使用通过参数传递的命令显示到datagridview

Public Sub displayTable(x As String)
    Dim command2 As New SqlClient.SqlCommand
    command2 = con.CreateCommand
    command2.CommandText = x
    command2.ExecuteNonQuery()

    Dim dt As New DataTable
    Dim da As New SqlDataAdapter(command2)
    da.Fill(dt)
    datagrid1.DataSource = dt
End Sub
5ssjco0h

5ssjco0h1#

最好将代码分为与窗体控件交互的用户界面代码和与数据库交互的数据访问代码。
这个 Using...End Using 块负责关闭和处理连接和命令,即使存在错误。
使用 Parameters 保护数据库不受sql注入的影响。我不得不猜测班级专栏的规模。检查数据库中的实际值。 .ExecuteNonQuery 用于插入、更新和删除而不是选择。您可以加载 DataTable 和一个读者。

Private ConStr As String = "Your connection string"
Private Function GetStudentsInfo(clss As String, d1 As Date, d2 As Date) As DataTable
    Dim dt As New DataTable
    Using con As New SqlConnection(ConStr),
            command2 As New SqlCommand("SELECT * FROM studentdb1 WHERE class = @Class AND date_registered BETWEEN @Date1 AND @Date2;", con)
        With command2.Parameters
            .Add("@Class", SqlDbType.VarChar, 100).Value = clss
            .Add("@Date1", SqlDbType.Date).Value = d1
            .Add("@Date2", SqlDbType.Date).Value = d2
        End With
        con.Open()
        dt.Load(command2.ExecuteReader)
    End Using
    Return dt
End Function

在ui中

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Validate input
    If ComboBox1.Text = "" OrElse ComboBox2.Text = "" Then
        MsgBox("Please Fill In The Details")
        Return
    End If
    Dim FirstDate, SecondDate As Date
    If Not Date.TryParse(Date1.Text, FirstDate) Then
        MessageBox.Show("Please enter a valid date.")
        Return
    End If
    If Not Date.TryParse(Date2.Text, SecondDate) Then
        MessageBox.Show("Please enter a valid date.")
        Return
    End If
    Dim dt As DataTable
    Try
        Select Case ComboBox1.Text
            Case "Students Info"
                dt = GetStudentsInfo(ComboBox2.Text, FirstDate, SecondDate)
            Case Else
                dt = Nothing
        End Select
    Catch ex As Exception
        MsgBox(ex.Message)
        dt = Nothing
    End Try
    DataGridView1.DataSource = dt
End Sub
uidvcgyl

uidvcgyl2#

正如维杰在评论中提到的:
删除“where”(和where date\ u registered between),后跟和
但我想指出的是: command2.ExecuteNonQuery() 将只提供受影响行的计数。你不需要它。
尝试使用 CommandParameters (您可能需要重新设计代码)

Dim sqlString = "SELECT * FROM studentdb1 WHERE class = @Class AND date_registered BETWEEN @StartDate AND @EndDate"
    Dim command2 As New SqlClient.SqlCommand
    command2 = con.CreateCommand
    command2.CommandText = sqlString
    command2.Parameters.Add("@Class", SqlDbType.VarChar).Value = ComboBox2.SelectedText
    command2.Parameters.Add("@StartDate", SqlDbType.Date).Value = date1.Text
    command2.Parameters.Add("@EndDate", SqlDbType.Date).Value = date2.Text

相关问题