简体   繁体   中英

MS Access SELECT query DatePart

i have some problem with my SELECT Query to MS Access .mdb file.

i am using VB.Net and have to send query like..

"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)

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..

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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