簡體   English   中英

使用組合框特定選項顯示數據庫中的數據:ALL SIZES

[英]Display data from database using combo box specific option: ALL SIZES

界面我正在嘗試將數據從數據庫顯示到 DataGridView,我成功地做到了,但我想以兩種方式顯示:1) 顯示所有記錄和 2) 顯示指定記錄。 我能夠使用兩個組合框顯示指定的記錄:通過從下拉列表中指定品牌和尺寸,數據顯示在 DataGridView 上。 但是我現在想從下拉列表中顯示所有品牌和所有尺寸,但它沒有顯示; 它只顯示其中一個查詢。 如何根據下拉列表的選擇來放置代碼顯示?

我已經放置了一個 if 語句來檢查用戶是否選擇了 ComboBox1: ALL TIRES 和 ComboBox2: ALL TIRES 然后它應該執行 ALL QUERY。 如果不是,並且用戶只選擇了一種類型的品牌和尺碼,那么它將只顯示另一個查詢。

界面: 這是代碼:

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star"

    ''connecting data grid with the database
    Dim Sda As New MySqlDataAdapter
    Dim dbdataset As New DataTable
    Dim bSource As New BindingSource


    Try
        MysqlConn.Open()
        Dim Query As String
        Dim All As String

        If ComboBox1.Text = "ALL TYRES" & ComboBox2.Text = "ALL SIZES" Then
            All = "select * from golden_star.sales"
            Command = New MySqlCommand(All, MysqlConn)

        End If

       Query = "select sale_id,date,brand,size,selling_unit_price,cost_unit_price,quantity,cost_of_goods,profit,total_cost_price from golden_star.sales where brand = '" + ComboBox1.Text + "' and size = '" + ComboBox2.Text + "' "

        Command = New MySqlCommand(Query, MysqlConn)







        Sda.SelectCommand = Command
        Sda.Fill(dbdataset)
        bSource.DataSource = dbdataset
        DataGridView1.DataSource = bSource
        Sda.Update(dbdataset)


        MysqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)

    Finally
        MysqlConn.Dispose()
    End Try

End Sub

您的 SQL 命令未選擇相同的列。

All = "從 golden_star.sales 中選擇 *"

Query = "select sale_id,date,brand,size, selling_unit_price, cost_unit_price, quantity, cost_of_goods,profit,total_cost_price from golden_star.sales 其中品牌 = '" + ComboBox1.Text + "' and size = '" + ComboBox2.Text + " ’”

你是如何定義網格的? 列是動態創建的還是固定的?

嘗試將 All 查詢更改為 select 與其他查詢相同的列。

All = "select sale_id,date,brand,size, selling_unit_price, cost_unit_price, quantity, cost_of_goods,profit,total_cost_price from golden_star.sales"

如果這不起作用,請顯示新代碼(帶有 else 語句)和 gridview 定義。 還要檢查您的 combobox 中是否定義了“ALL TYRES”。(應該是 TYPES?)

我猜這是一項培訓任務。 如果這是實際將在公共網站上使用的內容,則需要更改查詢。 在 SQL 命令中連接字符串是不安全的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM