繁体   English   中英

VB.NET MYSQL 使用 MySQL 显示数据错误

[英]VB.NET MYSQL Displaying Data using MySQL Error

您好,我正在尝试使用 MySQL 语法在 vb.net 中显示数据,这是我的 Mysql 语法

SELECT  COUNT(status) as 'Number of Grade School for the Month of January'
                                    FROM blhtraining.userinfo
                                    Where survey_at='Talisay' 
                                    and status='College' and Month(member_since)='1' and 
                                    Year(member_since)='2021' 

这段代码在 Mysql 中有效,但是当我在 vb.net 中像这样修改它时

 Dim count_gradeSchool1 As String = "Select Case COUNT(status) As 'Members'
                                            From training.userinfo
                                            Where survey_at='" & txtmonthlylocation.Text & "' 
                                            And status ='College' 
                                            And Month(member_since)='" & monthly_reports & "'  
                                            And YEAR(member_since)='" & txtmyear.Text & "' 
                                            And Day(member_since)='11'"



        da = New MySqlDataAdapter(count_gradeSchool1, mycon)
        dt = New DataTable()
        da.Fill(dt)
        lblgs1.Text = dt.Rows(0)("Members")

我收到了这个错误

您的 SQL 语法有错误; 检查与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的“作为”成员“来自 training.u”附近使用

我确定语法正确是声明的变量吗?

您的问题可能是因为您将 SQL 粘贴到代码中而没有先启动字符串,因此 VB 看到了“select”并通过添加“case”来帮助您。 所以,这里的代码......

  • ...已修复 SQL 语法

  • ...使用参数。 始终使用参数。 你不知道我每天说多少次,试图阻止未来 SQL 注入黑客的潮流。 编写不使用参数的代码会让你被解雇,或者你将不得不忍受在你的良心上编写容易被黑客攻击的代码的后果 永远不要跳过在你的 SQL 中使用参数,即使它“只是一个跟踪你奶奶的记录集合的应用程序”

  • ...不会在 where 子句中的列上调用函数 - 不要这样做; 这是对资源的巨大浪费,并扼杀了使用索引的机会。 总是,总是试图让表数据保持不变,不进行转换。 在 99% 的情况下,还有另一种编写查询的方法

  • ...使用 executescalar - 你只想要一个值,使用适配器/表毫无意义

  • ...不使用带有空格的列别名 - 如评论中所述 - 不要这样做; 格式化列名不是数据库的工作,而是前端的工作。

     Dim count_gradeSchool1 As String = "Select COUNT(*) as c FROM training.userinfo Where survey_at = @loc And status = 'College' And member_since = @ms" Using c = New MySqlCommand(count_gradeSchool1, mycon) c.Parameters.AddWithValue("@loc", txtmonthlylocation.Text) c.Parameters.AddWithValue("@ms", new Date(CInt(txtmyear.Text), CInt(monthly_reports), 11) c.Connection.Open() 'if it's not already open lblgs1.Text = c.ExecuteScalar().ToString() End Using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM