大佬教程收集整理的这篇文章主要介绍了使用C#指定的强制转换无效错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我运行它.我不知道如何解决它,请帮忙
sqlConnection con1 = new sqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString); sqlCommand cmd1 = new sqlCommand("SELEct cast(round(sum(cAST(Avgnumbers AS numeric(12,2))),2) as decimal(12,2)) AS [Average Days] from MainTable ",con1); cmd1.Connection = con1; con1.open(); Int32 result = (Int32)cmd1.ExecuteScalar(); con1.Close();
我猜它会返回一个decimal或一个double,你需要这样做:
Int32 result = (Int32)(doublE)cmd1.ExecuteScalar();
要么:
Int32 result = (Int32)(decimal)cmd1.ExecuteScalar();
[编辑回复下面提交的问题]
要保持小数值,只需执行以下操作:
decimal result = (decimal) cmd1.ExecuteScalar();
如果需要,可以将小数转换为double:
double result = (doublE)(decimal) cmd1.ExectuteScalar();
以上是大佬教程为你收集整理的使用C#指定的强制转换无效错误全部内容,希望文章能够帮你解决使用C#指定的强制转换无效错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。