简体   繁体   English

MS Access SELECT查询DatePart

[英]MS Access SELECT query DatePart

i have some problem with my SELECT Query to MS Access .mdb file. 我的SELECT查询到MS Access .mdb文件时遇到一些问题。

i am using VB.Net and have to send query like.. 我正在使用VB.Net,并且必须发送查询。

"SELECT d_date, d_tons, d_qty, d_cost FROM [deal] WHERE DatePart(""m"", [d_date]) = '" _
+ DTP.Value.Month.ToString + "' AND ([d_client] = '" + cBoxClient.Text + "')"

But it doesn't work.. No Error in compiling but this Query cannot SELECT any data. 但是它不起作用。编译时没有错误,但是此查询无法选择任何数据。

DTP is DateTimePicker, i select Month with DTP and filled some text into cBoxClient(ComboBox) DTP是DateTimePicker,我选择DTP月份,然后将一些文本填充到cBoxClient(ComboBox)中

What's wrong with that Query? 该查询出了什么问题? i have no idea because i always used MySQL and this is my first application development with MS Access.. 我不知道,因为我一直使用MySQL,这是我第一次使用MS Access开发应用程序。

Please HELP me. 请帮我。

Use parameterized query, that will save you from sql injection and complexity of converting specific data format (such as DateTime ) to it's string representation that is valid according to database specific culture. 使用参数化查询,可以避免SQL注入和将特定数据格式(例如DateTime )转换为根据数据库特定区域性有效的字符串表示形式的复杂性。 For example : 例如 :

Dim queryString = "SELECT d_date, d_tons, d_qty, d_cost FROM [deal] WHERE " & _ 
                  "DatePart(""m"", [d_date]) = ? AND ([d_client] = ?)"
OleDbCommand cmd = New OleDbCommand(queryString, connection)
cmd.Parameters.AddWithValue("@date", DTP.Value.Month)
cmd.Parameters.AddWithValue("@client", cBoxClient.Text)

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

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