请帮忙! 当我尝试对一列进行求和时,有两个条件就是错误:
标准表达式中的数据类型不匹配。
这是sql代码:
sql1 = "SELECT SUM(quantityofproduct) AS Expr1 FROM (SP001) where year = "2016" AND month = 1"
有4列:
1.ID
2.quantityofproduct
3.Year
4.month
SP001是talbe的名称。
我尝试使用日期列格式和& datetimepicker1.value / text&对于标准,但它没有工作。
感谢您的帮助。
答案 0 :(得分:-1)
如果字段类型为varchar(30),则可以使用引号''像这样:
sql1 = "SELECT SUM(quantityofproduct) AS Expr1 FROM (SP001) where year = '2016' AND month = '1'"
但是,我建议您将Column Year和Month的类型更改为Integer
,并使用Parameter来避免SQL注入:
Dim query As String = "SELECT SUM(quantityofproduct) AS Expr1 FROM (SP001) " &
"where year = @year And month = @month"
Dim cmd As New OleDbCommand(query, connection)
cmd.Parameters.AddWithValue("@year", YearValue)
cmd.Parameters.AddWithValue("@month", MonthValue)
Dim sumQuantity As Int32 = Convert.ToInt32(cmd.ExecuteScalar())
YearValue和MonthValue是搜索的年份和月份的值。