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

查看:239
本文介绍了从获得的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.Field<串GT(名称)?当然,第二种方式施放价值某种类型的,但一些其他的区别?哪种方法更好用?

And question is: 'What difference is between use row["name"] and row.Field<string>("name")? Of course second way cast value to some type, but is some other difference? Which method is better to use?

推荐答案

请参阅的备注部分,有描述主要区别:

See Remarks section, main differences described there:

数据集类表示的无效的DBNull 类的实例,价值
值。任何以价值访问的列的语言集成查询(LINQ)表达式
将产生
InvalidCastException的在运行时。此外,的DataSet 没有
支持的可为空类型。外地方法为
访问的列的可为空类型的支持。如果
DataSet中的基础值是值,返回可空类型将有
的值无效

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 为空和 T
引用类型或可空类型,返回类型为空。在
场的方法将不会返回值。

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.

外地方法不执行类型转换。如果需要输入
股转换,应首先获得通过实地方法
列值。则列值应转换为
另一种类型。

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中,字符串)第一次出现在.NET 3.5,它提供了指定行中的强类型访问每个列的值。实地方法还支持可为空类型。

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."

据我所知,行[名称] 收益对象 row.Field<串GT(名称)返回字符串
,我们不应该比较苹果和梨,因此,你应该问什么是更好的:

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:

行[名称]。的ToString() VS row.Field<串>(名称)
,答案是:他们是同一个

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

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

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