error CS0266: Cannot implicitly convert type 'object' to 'int'

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

error CS0266: Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

int dd= 6000; sqlCmdDefaultTime = new SqlCommand("myQuery", sqlCon); sqlDefaultTime = sqlCmdDefaultTime.ExecuteReader(); while (sqlDefaultTime.Read()) {       dd= sqlDefaultTime[1]; } 

how can i cast

回答1:

Simple cast to int:

dd = (int)sqlDefaultTime[1]; 


回答2:

Try this...

int.TryParse(sqlDefaultTime[1].ToString(), out dd); 

in the event that the parse is successful dd will now be a new value.

Unless of course the object is an int already, the you can just cast it...

dd = (int)sqlDefaultTime[1]; 


回答3:

Instead of the indexer try to use the GetXXX methods of SqlDataReader:

dd = sqlDefaultTime.GetInt32(1); 

More GetXXX methods here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx



回答4:

Integer.parseInt();

a few others such as TryParse and TryParseExact provide more functionality and control.

dd= Integer.parseInt(sqlDefaultTime[1]);  

should do it, provided you can guarantee the value being returned is always an int.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!