我在投放时出错,请告诉我为什么 [英] i m getting error of casting please tell me why
本文介绍了我在投放时出错,请告诉我为什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
DateTime date = (DateTime)com.ExecuteScalar();//HERE M GETTING ERROR OF CASTING
if(date!=DateTime.Now.Date)
{
}
[edit]添加了代码块[/edit]
[edit]code block added[/edit]
推荐答案
尝试一下.
Try this.
using( SqlConnection cn =new SqlConnection(ConfigurationManager.ConnectionStrings["Conection Name here"].ConnectionString))
{
SqlCommand com=new SqlCommand();
com = new SqlCommand("SELECT MAX(createddate) FROM Payout_Master WHERE sponcorid='" + code + "'", con);
con.Open();
var OrderDate = (String)com.ExecuteScalar();
if(!string.IsNullOrEmpty(OrderDate))
{
DateTime oDate = Convert.ToDateTime(OrderDate);
// rest of the code.
}
}
如果给定代码根本没有创建日期,则结果将为DBNull(并且不能转换为DateTime).检查结果类型并进行相应转换.
其他说明:
-WHERE sponcorid=''" + code + "''
->你知道鲍比表"吗?搜索google,它是第一个热门.
-您将sponcorid与代码进行了比较...但是id并不是代码,反之亦然.
祝你好运!
If there is no created date at all for the given code, the result will be DBNull (and cannot be cast to a DateTime). Check the result type and convert accordingly.
Some other remarks:
-WHERE sponcorid=''" + code + "''
-> You know "Bobby Tables"? Search google, its the first hit.
- you compare sponcorid to code... but an id isn''t a code, and vice versa.
Good luck!
您不能将对象(ExecuteScalar()的返回类型)直接转换为DateTime.
也许您可以尝试以下操作:
Hi,
you cannot cast object (the return type of ExecuteScalar()) to DateTime directly.
Maybe you can try something like the following:
string str = Convert.ToString(com.ExecuteScalar());
DateTime date = DateTime.Parse(str);
这篇关于我在投放时出错,请告诉我为什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文