如何迭代数据表

时间:2022-09-23 00:13:51

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

我需要遍历一个DataTable。我有一个专栏叫做ImagePath。

When I am using DataReader I 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获得相同的结果?

4 个解决方案

#1


206  

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.

…假设连接是打开的,命令被正确设置。我也没有检查语法,但是它应该会让你明白。

#2


24  

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或序号)。

-> = contains.

- > =包含。

#3


15  

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;
}

#4


3  

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 ";
            }
        }

#1


206  

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.

…假设连接是打开的,命令被正确设置。我也没有检查语法,但是它应该会让你明白。

#2


24  

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或序号)。

-> = contains.

- > =包含。

#3


15  

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;
}

#4


3  

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 ";
            }
        }