无法将参数值从SqlParameter转换为DateTime [英] Failed to convert parameter value from a SqlParameter to DateTime
本文介绍了无法将参数值从SqlParameter转换为DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的代码如下:但是我收到错误消息:无法将参数值从SqlParameter转换为DateTime".
任何建议plz将不胜感激.
Hi All,
my code is below: but i am getting error:"Failed to convert parameter value from a SqlParameter to DateTime".
Any suggestion plz will be appreciated.
DataTable dt = new DataTable();
try
{
string spName = "spResult1";
List<sqlparameter> paramlist=new List<sqlparameter>();
paramlist.Add(new SqlParameter("@fromDate",obj.dateFrom));
paramlist.Add(new SqlParameter("@toDate",obj.dateTo));
DataSet ds = SqlHelper.ExecuteDataset(objconn._connectionString, spName, paramlist.ToArray());
dt = ds.Tables[0];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
return dt;</sqlparameter></sqlparameter>
推荐答案
您的问题有两个问题;
1.obj.dateFrom
和obj.dateTo
,它们返回什么,一个字符串或一个DateTime对象,如果它返回一个字符串,则格式可能会导致问题,具体取决于dd/MM/yyyy和MM/dd/yyyy格式.最好返回DateTime而不是字符串.
2.SqlHelper
类,它如何处理DateTime对象?
简而言之,在处理DateTime值时,请使用实际的DateTime对象作为参数,或者如果使用字符串,请使用dd MMM yyyy或yyyyMMdd格式,这样您就不必处理歧义格式.
There are two issues with your question;
1.obj.dateFrom
andobj.dateTo
, what do these return, a string or a DateTime object, if it returns a string the format can cause problems depending on the format dd/MM/yyyy vs MM/dd/yyyy you would be better off returning a DateTime instead of a string.
2. TheSqlHelper
class, how does this process DateTime objects?
In short when dealing with DateTime values either use an actual DateTime object as the parameter or if using a string use the dd MMM yyyy or yyyyMMdd formats so that you do not have to deal with ambiguous formats.
如果您查看错误,它会显示无法将参数值从和 SqlParameter 转换为DateTime"
因此,您可以在某个地方尝试将SqlParameter
设置为DateTime
变量,而不必在发布的代码中进行设置.
如果您进行调试,则在哪一行引发异常(请记住在调试时进入SqlHelper.ExecuteDataset
)?
If you have a look at the error, it says "Failed to convert parameter value from a SqlParameter to DateTime"
So somewhere you try to set aSqlParameter
to aDateTime
variable, not necessarily in the code you''ve posted.
If you debug, at what line the exception is thrown (remember to step intoSqlHelper.ExecuteDataset
when debugging)?
这篇关于无法将参数值从SqlParameter转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文