可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.