代码大致如下:
SqlDataAdapter sa = ......
DataTable dt = new DataTable();
sa.Fill(dt);DataRow read = dt.Rows[0];
DataRow next = dt.Rows[1];我的理解是,read和next分别是表中的第1和第2行。
现在我如果想分别读出该行具体某个列的内容,应该如何去做呢?
比如:
read.Column[x],有没有这样的用法呢?多谢。
SqlDataAdapter sa = ......
DataTable dt = new DataTable();
sa.Fill(dt);DataRow read = dt.Rows[0];
DataRow next = dt.Rows[1];我的理解是,read和next分别是表中的第1和第2行。
现在我如果想分别读出该行具体某个列的内容,应该如何去做呢?
比如:
read.Column[x],有没有这样的用法呢?多谢。
read[0] or read["xx"],
有很多方法取得字段值,最普通的是使用index和字段名
read[index] 或者 read["字段名"]参考:
http://msdn.microsoft.com/en-us/library/system.data.datarow_members.aspx
中的
Properties下的
Item[DataColumn]
Item[Int32]
Item[String]
....如果是net 3.5以上
还可以使用强类型访问
参考上面链接中的
Extension Methods下的
Field<T>(DataColumn)
Field<T>(Int32)
Field<T>(String)
...
第一行第一列的值
DataRow read = dt.Rows[0]; read["column0"] \\第一行第一列的值
read["column1"] \\第一行第二列的值DataRow next = dt.Rows[1]; next["column0"] \\第二行第一列的值
next["column1"] \\第二行第二列的值或者 dt.Rows[0]["column0"] \\第一行第一列的值
dt.Rows[0]["column1"] \\第一行第二列的值 dt.Rows[1]["column0"] \\第二行第一列的值
dt.Rows[1]["column1"] \\第二行第二列的值
菲律宾太阳城www.sun600.com
dt.Rows[i]["字段名"]
DataRow read = dt.Rows[0];
int i = read.Field<int>(0);