如何解决将日期时间值传递给参数化查询错误 [英] how to solve Passing DateTime value to paramaterized query Error
本文介绍了如何解决将日期时间值传递给参数化查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的查询字符串
here is my query string
SELECT
Payment
,Balance
,PatientNo
FROM
[GP_DB].[dbo].[GP]
where GP.GPDate= (SELECT CONVERT(VARCHAR(24),@GPDate,103))
GPDate是日期类型列,而不是DateTime
我传递这样的参数
GPDate is a Date type column, not DateTime
and i pass parameter like this
SqlCommand cmd_select_treatment = new SqlCommand(select_cashReceipt, sqlconn);
cmd_select_treatment.Parameters.AddWithValue(
"@GPDate"
,Convert.ToDateTime(dateTimePicker1.Value));
但发生以下错误
从字符转换日期和/或时间时转换失败string。
but the following error occur
Conversion failed when converting date and/or time from character string.
推荐答案
您好b $ b
如果它是数据库中的日期列,您可以尝试这样做..
将查询更改为
Hi
If it is a date column in database you can try like this..
change the query to
SELECT Payment ,Balance ,PatientNo FROM [GP_DB].[dbo].[GP] where GP.GPDate= @GPDate
从DateTime对象中删除时间为
Remove the Time from the DateTime object as
DateTime gpDate = Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString());
cmd_select_treatment.Parameters.AddWithValue( "@GPDate" , gpDate);
这篇关于如何解决将日期时间值传递给参数化查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文