gpt4 book ai didi

mysql - 在 VB.Net 中搜索过滤 Mysql/Gridview

转载 作者:行者123 更新时间:2023-11-30 22:48:45 24 4
gpt4 key购买 nike

我有 2 个文本框“TextBox1”和“TextBox2”以及一个名为“Button1”的提交按钮在它下面我有一个标签“Label1”

我需要做的是在“Input1 & Input2”(yyyy-mm-dd)中的日期之间搜索并显示一个值(它只是计算表格中的记录)

这是我当前的查询

Dim c As New MySqlConnection("Server=localhost;Database=test;UID=test;PWD=test;")
c.Open()
Dim com As New MySqlCommand("SELECT COUNT(*) as c FROM toutcome WHERE AffID = '0001' AND CompletedDate >= CURDATE();", c)
Dim myReader As MySqlDataReader = com.ExecuteReader(CommandBehavior.CloseConnection)
myReader.Read()
Label1.Text = myReader.Item(0).ToString()

myReader.Close()

这是我试图用来完成上述任务的查询,它根本不起作用。

Dim c As New MySqlConnection("Server=localhost;Database=mdxmain;UID=MerryWoodTest;PWD=H0r$hamTest;")
c.Open()
Dim com As New MySqlCommand("SELECT COUNT(*) as c FROM toutcome WHERE AffID = '0001' AND WHERE CompletedDate >= '" & TextBox1.Text & "' AND CompletedDate <= '" & TextBox2.Text & "'", c)
Dim myReader As MySqlDataReader = com.ExecuteReader(CommandBehavior.CloseConnection)
myReader.Read()
Label11.Text = myReader.Item(0).ToString()

myReader.Close()

top 查询有效,我得到了一个值。

底层查询错误为

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE CompletedDate >= '' AND CompletedDate <= ''' at line 1]

最佳答案

该行中的 WHERE 太多了

... FROM toutcome WHERE AffID = '0001' AND WHERE ....
^^^^^

当然,更好的方法是使用参数化查询使命令文本更具可读性并防止 Sql 注入(inject),并在一次性对象周围添加 Using 语句以在完成使用它们时释放它们的资源

Dim commandText= "SELECT COUNT(*) as c FROM toutcome " & _
"WHERE AffID = '0001' AND CompletedDate >= @init " & _
" AND CompletedDate <= @ending"
Using c = New MySqlConnection(".........")
Using com = new MySqlCommand(commandText, c)
c.Open()
com.Parameters.Add("@init", MySqlDbType.Date).Value = Convert.ToDateTime(TextBox1.Text)
com.Parameters.Add("@ending", MySqlDbType.Date).Value = Convert.ToDateTime(TextBox2.Text)
Using myReader = com.ExecuteReader(CommandBehavior.CloseConnection)
if myReader.Read() Then
Label11.Text = myReader.Item(0).ToString()
End If
End Using
End Using
End Using

关于mysql - 在 VB.Net 中搜索过滤 Mysql/Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28765800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com