简体   繁体   English

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

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

Hello I'm trying to display data in vb.net using MySQL syntax here is my Mysql syntax您好,我正在尝试使用 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' 

And this code works in Mysql but when i modify it like this in vb.net这段代码在 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")

I recieved this error我收到了这个错误

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'As 'Members' From training.u' at line 1检查与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的“作为”成员“来自 training.u”附近使用

I'm sure the syntax is correct is it the variable declared?我确定语法正确是声明的变量吗?

Your problems probably came about because you pasted the SQL into your code without starting a string first, so VB saw "select" and helped you out by adding "case".您的问题可能是因为您将 SQL 粘贴到代码中而没有先启动字符串,因此 VB 看到了“select”并通过添加“case”来帮助您。 So, here is code that...所以,这里的代码......

  • ...has fixed SQL syntax ...已修复 SQL 语法

  • ...uses parameters. ...使用参数。 Always use parameters.始终使用参数。 You've no idea how many times a day I say this, trying to stem the tide of future SQL injection hacks.你不知道我每天说多少次,试图阻止未来 SQL 注入黑客的潮流。 Writing code that doesn't use parameters will get you fired, or you'll have to live with the consequences of writing hack prone code on your conscience.编写不使用参数的代码会让你被解雇,或者你将不得不忍受在你的良心上编写容易被黑客攻击的代码的后果 Don't ever skip on using parameters in your SQLs, even if it's "only an app to track your grandma's record collection"永远不要跳过在你的 SQL 中使用参数,即使它“只是一个跟踪你奶奶的记录集合的应用程序”

  • ...doesn't call functions on columns in the where clause - don't do it; ...不会在 where 子句中的列上调用函数 - 不要这样做; it's a huge waste of resources and kills opportunities to use indexes.这是对资源的巨大浪费,并扼杀了使用索引的机会。 Always, always try to leave table data alone, untransformed.总是,总是试图让表数据保持不变,不进行转换。 In 99% of cases there is another way to write the query在 99% 的情况下,还有另一种编写查询的方法

  • ...uses executescalar - you only want one value, pointless using an adapter/table for it ...使用 executescalar - 你只想要一个值,使用适配器/表毫无意义

  • ...doesn't use column alises with spaces in - as noted in the comments - don't do it; ...不使用带有空格的列别名 - 如评论中所述 - 不要这样做; it's not the database's job to format your column names, it's the front end's job.格式化列名不是数据库的工作,而是前端的工作。

     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