如何使用VB .NET使用radiobutton在datagridview中搜索和过滤数据,我的数据库是mysql [英] How can I search and filter data in datagridview using radiobutton using VB .NET and my databse is mysql

查看:64
本文介绍了如何使用VB .NET使用radiobutton在datagridview中搜索和过滤数据,我的数据库是mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中没有错误,但数据网格视图中没有数据显示。提前谢谢



我尝试过:



No error in my code but no data display in my datagridview. thank you in advance

What I have tried:

Private Sub YearlyREp()
        Dim con As MySqlConnection = New MySqlConnection("server=localhost;user id=root;password=;database=itbcis; Convert Zero DateTime=True")
        Dim Content As String

        If (RBproducts.Checked = True) Then
            Content = "Bondpaper" & "Ballpen" & "Pentelpen" & "Ink" & "Manila Paper" & "Envelop" & "Folder" & "Pencil"
            RBservices.Checked = False
        Else : RBservices.Checked = True
            Content = "Binding" & "Encoding" & "Photocopy" & "Print" & "Scan"
            RBproducts.Checked = False
        End If

        Dim sql As MySqlCommand = New MySqlCommand("Select DATE_FORMAT(Date, '%m/%Y') as Month, ServicesOrProduct, sum(Quantity) as Quantity, sum(Total) as Total from sales where Year(Date) = '" & cbYear.Text & "' and ServicesOrProduct = '" & Content & "' group by ServicesOrProduct, Month(Date) ORDER BY Month(Date)", con)
        Dim ds As DataSet = New DataSet()
        Dim da As MySqlDataAdapter = New MySqlDataAdapter()
        con.Open()
        da.SelectCommand = sql;
        da.Fill(ds, "sales")
        DGVYearly.DataSource = ds
        DGVYearly.DataMember = "sales"
        con.Close()
    End Sub

推荐答案

MySqlCommand = New MySqlCommand("Select DATE_FORMAT(Date, '%m/%Y') as Month, ServicesOrProduct, sum(Quantity) as Quantity, sum(Total) as Total from sales where Year(Date) = '" & cbYear.Text & "' and ServicesOrProduct = '" & Content & "' group by ServicesOrProduct, Month(Date) ORDER BY Month(Date)", con)



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


也许你可以使用BindingSource.Filter,参见: BindingSource.Filter属性(System.Windows.Forms) [ ^ ]

并且: BindingSource.Filter [ ^ ]
Maybe you can use BindingSource.Filter, see: BindingSource.Filter Property (System.Windows.Forms)[^]
And: BindingSource.Filter[^]


你真的需要回去学习如何s转向工作。你的两条内容行看起来像这样:

You really need to go back and learn how strings work. Your two "Content" lines that look like this:
Content = "Bondpaper" & "Ballpen" & "Pentelpen" & "Ink" & "Manila Paper" & "Envelop" & "Folder" & "Pencil"
Content = "Binding" & "Encoding" & "Photocopy" & "Print" & "Scan"

实际上就是这样:

are actually this:

Content = "BondpaperBallpenPentelpenInkManila PaperEnvelopFolderPencil"
Content = "BindingEncodingPhotocopyPrintScan"



我严重怀疑你的数据库是否有像这样的ServicesOrProduct项目。



您也在处理日期作为查询中的字符串:


I seriously doubt your database has ServicesOrProduct items that look like those.

You are also treating dates as string in the query:

...where Year(Date) = '" & cbYear.Text & "' and...



删除单引号,因为你实际上是在比较整数值,而不是字符串的整数。


Remove the single quotes because you're actually comparing integer values, not an integer to a string.

...where Year(Date) = " & cbYear.Text & " and...


这篇关于如何使用VB .NET使用radiobutton在datagridview中搜索和过滤数据,我的数据库是mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆