从DataRow获取值之间的差异 [英] difference between getting value from DataRow

查看:192
本文介绍了从DataRow获取值之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码:

    DataTable table = new DataTable();

    // ...
    // insert column to table

    table.Columns.Add("name");

    // ...
    // insert value to table

    foreach (DataRow row in table.Rows) {
         row["name"];
         row.Field<string>("name");     
    }

我的问题是:


  • 使用 row [name] row.Field< string>(名称)?当然,第二种方式是将某种类型的价值转换成一种类型,但还有另一个区别吗?

  • 最好使用哪种方法?

  • Is there a difference between using row["name"] and row.Field<string>("name")? Of course, the second way cast value to some type, but is there another difference?
  • Which method is better to use?

推荐答案

请参阅备注部分,其中描述的主要差异:

See Remarks section, main differences described there:


DataSet 类表示 null 值,其值为 DBNull 类。语言集成查询(LINQ)表达式
访问具有 null 值的列将在运行时生成
InvalidCastException 。另外, DataSet 不支持
支持可空的类型。 Field方法支持
访问列为可空的类型。如果
DataSet中的基础值为Value,则返回的可空类型的值为
null

The DataSet class represents null values with the Value instance of the DBNull class. A Language-Integrated Query (LINQ) expression that accessed a column with a null value would generate a InvalidCastException at run time. Additionally, DataSet does not support nullable types. The Field method provides support for accessing columns as nullable types. If the underlying value in the DataSet is Value, the returned nullable type will have a value of null.

如果指定的 DataColumn 的值为null,并且 T
引用类型或可空类型,返回类型将为 null
Field方法不会返回值。

If the value of the specified DataColumn is null and T is a reference type or nullable type, the return type will be null. The Field method will not return Value.

Field方法不执行类型转换。如果需要输入
转换,则应首先使用Field方法获取
的列值。然后将列值转换为
另一种类型。

The Field method does not perform type conversions. If type conversion is required, you should first obtain the column value by using the Field method. The column value should then be converted to another type.

最后一段说明了我经常看到的数字作为字符串存储在数据库中,因此在数据检索时需要转换 varchar int ,所以在这种情况下使用DataColumn更好,例如:

The last paragraph makes a point as I've often seen numbers stored as strings in database, therefore varchar to int conversion would be required on data retrieval, so in this case using DataColumn is better, e.g.:

int test = row.Field<int>("Test"); // InvalidCastException
int test = Convert.ToInt32(row["Test"]); // Works like a charm

DataRowExtensions.Field< T>方法(DataRow,String)首先出现在.NET 3.5中,它为指定行中的每个列值提供强类型访问。Field方法还支持可空类型

DataRowExtensions.Field<T> Method (DataRow, String) first appeared in .NET 3.5 and it "provides strongly-typed access to each of the column values in the specified row. The Field method also supports nullable types."

Afaik, row [name] 返回 code>, row.Field< string>(name)返回一个 String
我们不应该比较苹果和梨,所以你应该问更好的是:

Afaik, row["name"] returns object, row.Field<string>("name") returns a String. We shouldn't be comparing apples and pears, hence you should be asking what's better:

row [name]。 ToString() vs row.Field< string>(name)
,答案是:他们是一样的

row["name"].ToString() vs row.Field<string>("name") and the answer is: they're same.

这篇关于从DataRow获取值之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆