froeach(DataRowView drv in dv)中的各参数意思?

来源:百度知道 编辑:UC知道 时间:2024/07/02 10:28:07
froeach(DataRowView drv in dv)
{
for(int i=0;i<dv.table.colum.count;i++)
{
Console.writeln(drv[i]+"\t");
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sqlclient;
namespace Data_View
{
class Program
{
static ovid main(String[]args)
{
string connectionString="data source=pic114;initial catalog=hr;user id=sa ;password=123456";
string sSQL="Select*from Employee";
Sqlconnection conn=new Sqlconnection(connectionString);
try{
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=new SqlCommand(sSQL,conn);
DataSet ds=new DataSet();
da.Fill(ds,"Employee");
DataTable dt=ds.Tables["Employee"];
DataView dv=new DataView(dt,"MaritalStatus='M'","MaritalStatus",DataViewRowState.CurrentRows);
froeach(DataRowView drv

DataRowView这是一个.NET封装类,具体说明可以看一个帮助,
drv是上面那个类的一个对像
in是一个运算符,和我们用的+、-、*、/是一样的,你可以理解为在……这内
dv是一个DataRowView类对像的集合,也就是在dv中包含了好多DataRowView的对像,然后将这些DataRowView对像的值赋给drv这些个临时的DataRowView对像,然后你就可以很方便对这些对像进行操作做。
下面是一个同上面的类例子
数组:int[] aa=new int[]{1,2,3};
从aa中取出1,2,3进行相加,这时就可以这样写
int total=0;
foreach(int a in aa)
{
total+=a;
}
明白了吗?

froeach(DataRowView drv in dv)
{
for(int i=0;i<dv.table.colum.count;i++)
{
Console.writeln(drv[i]+"\t");
}
}

其实很简单,这就是一个froeach遍历。。。然后在中间嵌套了一个For循环
DataRowView drv 就表示定义的一个DataRow的视图 但是这个视图在视图数组dv中 循环一次 就把dv 的值放到drv 中
在这个时候使用For循环 循环了一个一个表 然后输出 一个行视图

dv 应该是各DataRowView 列表之类的数组。
froeach(DataRowView drv in dv)
就是每次从dv中取出一个DataRowView对象放到drv变量里,然后下面就可以使用个这个DataRowView了。
一般用于不确定数目的集合的遍历。

dv应该是dataview 这个循环是遍历dv里的每一行,DataRowView 是dv 每一行数据的数据类型,drv是每个DataRowView类型的参数,

DataRowView这是一个.NET封装