0

我有这段代码

Int32 clientiid = row["CodCliente "].ToInt();

但这被标记为错误提示并且您缺少指令或参考 我应该添加什么才能使 ToInt 工作?我已经使用了这个参考。先感谢您

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;


Int32 clientiid = row["CodCliente "].ToInt();
4

7 回答 7

3

C#中没有ToInt()方法。

如果你int在 type 的表达式中有一个装箱的值object,你可以简单地转换它:

int clientId = (int)row["CodCliente"];

如果您不确定表达式的运行时类型是什么(例如,它可能是longor decimal,甚至是 a string),您可以调用Convert.ToInt32(),它会找出它实际上是什么,然后int为您将其转换为 an 。

有关更多信息,请参阅这篇出色的博客文章

于 2013-04-19T17:20:40.470 回答
2

您也可以使用以下方法,

Convert.ToInt32(row["CodCliente"]);
于 2013-04-19T17:23:52.430 回答
1

C# 编译器告诉您它无法识别.ToInt()方法。所以它应该被声明或使用这种方法的程序集应该在文档的标题中引用。

你可以写这样的东西(基于行数据返回类型的扩展方法);ifrow["index"]返回字符串,例如:

public static int ToInt(this string number)
{
    return int.Parse(number);
}
于 2013-04-19T17:22:20.493 回答
0

你调用的方法是错误的。你需要做(假设值是一个字符串)Convert.ToInt32(row["CodCliente "])

(int)row["CodCliente "]你也可以像我喜欢的那样投射。

于 2013-04-19T17:20:50.067 回答
0

没有Object.ToInt()方法。您需要使用:

Convert.ToInt32(row["CodCliente "]);

或者

int.Parse(row["CodCliente "]);

或者,如果该值已经是行中的 int,您可以简单地转换:

(int)row["CodeCliente "];
于 2013-04-19T17:21:17.043 回答
0

像这样使用转换:

int clientId = Convert.ToInt32(row["CodCliente"]);
于 2013-04-19T17:21:55.397 回答
0

什么日期类型是rowrow[string index]索引器返回什么数据类型?该类型有ToInt()方法吗?该类型是否有ToInt()可用的扩展方法?

类或结构是否可转换为 int(例如,是否row["foo"] is int返回 true?如果是这样,您可以简单地转换它,因此:int x = (int) row["foo"].

如果 object 是字符串,则可以使用int.Parse()or int.TryParse()

于 2013-04-19T17:26:41.490 回答