简体   繁体   English

错误CS0266:无法将类型'object'隐式转换为'int'

[英]error CS0266: Cannot implicitly convert type 'object' to 'int'

error CS0266: Cannot implicitly convert type 'object' to 'int'. 错误CS0266:无法将类型'object'隐式转换为'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 我怎么能投

简单转换为int

dd = (int)sqlDefaultTime[1];

Try this... 尝试这个...

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

in the event that the parse is successful dd will now be a new value. 如果解析成功, dd现在将是一个新值。

Unless of course the object is an int already, the you can just cast it... 当然除非对象已经是int,你可以把它投出来......

dd = (int)sqlDefaultTime[1];

Instead of the indexer try to use the GetXXX methods of SqlDataReader: 而不是索引器尝试使用SqlDataReader的GetXXX方法:

dd = sqlDefaultTime.GetInt32(1);

More GetXXX methods here: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx 这里有更多GetXXX方法: http//msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx

Integer.parseInt(); 的Integer.parseInt();

a few others such as TryParse and TryParseExact provide more functionality and control. 其他一些如TryParse和TryParseExact提供了更多的功能和控制。

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

should do it, provided you can guarantee the value being returned is always an int. 应该这样做,只要你能保证返回的值总是一个int。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39;。 存在显式转换(您是否缺少演员表?) - Error: CS0266 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?) 条件运算符。 错误CS0266无法将类型&#39;int&#39;隐式转换为&#39;byte&#39; - Conditional operator. Error CS0266 Cannot implicitly convert type 'int' to 'byte' 错误 CS0266:无法将类型“float”隐式转换为“int”。 存在显式转换(您是否缺少演员表?) - error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) C#错误CS0266:无法将类型“狗”隐式转换为“ Rottweiler” - C# error CS0266: Cannot implicitly convert type 'Dog' to 'Rottweiler CS0266 无法转换类型 - CS0266 cannot convert type CS0266 C# 无法将类型“UnityEngine.Object”隐式转换为“”。 存在显式转换(您是否缺少演员表?) - CS0266 C# Cannot implicitly convert type 'UnityEngine.Object' to ''. An explicit conversion exists (are you missing a cast?) 错误 CS0266 无法将类型“Newtonsoft.Json.Linq.JObject”隐式转换为“字符串”。 存在显式转换(您是否缺少演员表?) - Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?) CS0266-无法将类型“ System IList”隐式转换为“ System ArrayList”-软件测试 - CS0266 - Cannot implicitly convert type “System IList” to “System ArrayList” - Software Testing CS0266 无法隐式转换类型 'System.Collections.Generic.IEnumerable<char> '到'字符串'</char> - CS0266 cannot implicitly convert type 'System.Collections.Generic.IEnumerable<char>' to 'string' 错误CS0029:无法将类型&#39;int&#39;隐式转换为&#39;Score&#39; - error CS0029: Cannot implicitly convert type 'int' to 'Score'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM