简体   繁体   English

VB.NET和MySQL:显示MonthName的DataGrid错误

[英]VB.NET & MySQL : DataGrid error displaying MonthName

Having trouble displaying MonthName in a DataGridView from data in MySQL 无法从MySQL中的数据在DataGridView中显示MonthName

Query: 查询:

SELECT ItemName, Description, PurchaseDate, MONTHNAME(PurchaseDate) AS PurchaseMonth FROM tblItems

Error: 错误:

System.ArguementException:Parameter is not valid System.ArguementException:参数无效

Displaying the month number, using MONTH(PurchaseDate) works perfect but when I try to show the month name I get a datagrid error. 使用MONTH(PurchaseDate)显示月份号可以完美地工作,但是当我尝试显示月份名称时,出现了datagrid错误。 When I run the SQL in MySQL it returns the expected results. 当我在MySQL中运行SQL时,它将返回预期结果。

When I add in a DataError event it just shows a red exclimation mark in the fields where MONTHNAME(PurchaseDate) should be. 当我添加DataError事件时,它仅在MONTHNAME(PurchaseDate)应该位于的字段中显示一个红色的感叹号。

Solved it. 解决了。 Even though MonthName(PurchaseDate) worked when run directly against the database, I had to use Cast in the SQL string when passing in from VB.NET 即使MonthName(PurchaseDate)在直接对数据库运行时也可以使用,但从VB.NET传入时我仍必须在SQL字符串中使用Cast

Amended SQL: 修改后的SQL:

SELECT ItemName, Description, PurchaseDate, CAST(MONTHNAME(PurchaseDate) AS CHAR) AS PurchaseMonth FROM tblItems

Why do you want to do that? 你为什么要这么做?

Just load the raw date and use format string to get the name 只需加载原始日期并使用格式字符串即可获取名称

dim purchaseDate as DateTime = DateTime.Now

dim purchaseMonth as String = purchaseDate.Format("MMMM")
' or
dim purchaseMonth as String = String.Format("{0}", purchasDate")

Databindings also support formatting. 数据绑定还支持格式化。

This method has the advantage, that it respects the locals from the users environment: 此方法的优点是,它尊重用户环境中的本地人:

' english: July
' german:  Juli

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

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