从 DataRow 获取值的区别 [英] difference between getting value from DataRow

查看:16
本文介绍了从 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("name") 有区别吗?当然,第二种方式将值转换为某种类型,但还有另一个区别吗?
  • 哪种方法更好用?
  • 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 类用 Value 实例表示 nullDBNull 类的.语言集成查询 (LINQ) 表达式访问具有 null 值的列将生成一个InvalidCastException 在运行时.此外,DataSet 不会支持 nullable 类型.Field 方法提供支持以可为空类型访问列.如果基础价值在DataSet 为 Value,返回的可为空类型的值为.

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.这字段方法不会返回值.

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 方法不执行类型转换.如果输入需要转换,您应该首先通过以下方式获取列值使用字段方法.然后应将列值转换为另一种.

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方法 (DataRow, String) 首次出现在 .NET 3.5 中,它提供对指定行中每个列值的强类型访问.Field 方法还支持 nullable 类型."

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"] 返回 objectrow.Field("name") 返回一个 字符串.我们不应该比较苹果和梨,因此你应该问哪个更好:

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("name")答案是:它们是一样的.

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

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

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