C# 如何遍历数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1774498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:42:22  来源:igfitidea点击:

How to iterate through a DataTable

c#asp.net

提问by happysmile

I need to iterate through a DataTable. I have an column there named ImagePath.

我需要遍历一个DataTable. 我有一个名为ImagePath.

When I am using DataReaderI do it this way:

当我使用时,DataReader我这样做:

SqlDataReader dr = null;
dr = cmd.ExecuteReader();
while (dr.Read())
{
    TextBox1.Text = dr["ImagePath"].ToString();
}

How can I achieve the same thing using DataTable?

我怎样才能实现同样的事情DataTable

采纳答案by Justin Niessner

DataTable dt = new DataTable();

SqlDataAdapter adapter = new SqlDataAdapter(cmd);

adapter.Fill(dt);

foreach(DataRow row in dt.Rows)
{
    TextBox1.Text = row["ImagePath"].ToString();
}

...assumes the connection is open and the command is set up properly. I also didn't check the syntax, but it should give you the idea.

...假设连接已打开并且命令设置正确。我也没有检查语法,但它应该给你这个想法。

回答by shahkalpesh

foreach (DataRow row in myDataTable.Rows)
{
   Console.WriteLine(row["ImagePath"]);
}

I am writing this from memory.
Hope this gives you enough hint to understand the object model.

我是凭记忆写这个的。
希望这能给你足够的提示来理解对象模型。

DataTable-> DataRowCollection-> DataRow(which one can use & look for column contents for that row, either using columnName or ordinal).

DataTable-> DataRowCollection-> DataRow(可以使用 & 查找该行的列内容,使用 columnName 或 ordinal)。

-> = contains.

-> = 包含。

回答by Lee

You can also use linq extensions for DataSets:

您还可以为数据集使用 linq 扩展:

var imagePaths = dt.AsEnumerble().Select(r => r.Field<string>("ImagePath");
foreach(string imgPath in imagePaths)
{
    TextBox1.Text = imgPath;
}

回答by baymax

The above examples are quite helpful. But, if we want to check if a particular row is having a particular value or not. If yes then delete and break and in case of no value found straight throw error. Below code works:

上面的例子很有帮助。但是,如果我们想检查特定行是否具有特定值。如果是,则删除并中断,如果没有发现值,则直接抛出错误。下面的代码有效:

foreach (DataRow row in dtData.Rows)
        {
            if (row["Column_name"].ToString() == txtBox.Text)
            {
                // Getting the sequence number from the textbox.
                string strName1 = txtRowDeletion.Text;

                // Creating the SqlCommand object to access the stored procedure
                // used to get the data for the grid.
                string strDeleteData = "Sp_name";
                SqlCommand cmdDeleteData = new SqlCommand(strDeleteData, conn);
                cmdDeleteData.CommandType = System.Data.CommandType.StoredProcedure;

                // Running the query.
                conn.Open();
                cmdDeleteData.ExecuteNonQuery();
                conn.Close();

                GetData();

                dtData = (DataTable)Session["GetData"];
                BindGrid(dtData);

                lblMsgForDeletion.Text = "The row successfully deleted !!" + txtRowDeletion.Text;
                txtRowDeletion.Text = "";
                break;
            }
            else
            {
                lblMsgForDeletion.Text = "The row is not present ";
            }
        }