如何通过一个DataTable迭代 [英] How to iterate through a DataTable

查看:150
本文介绍了如何通过一个DataTable迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过数据表进行迭代。我有一个列有一个名为的ImagePath

当我使用的DataReader 我做这种方式:

  SqlDataReader的博士= NULL;
博士= cmd.ExecuteReader();
而(dr.Read())
{
    TextBox1.Text =博士[的ImagePath]的ToString()。
}

我如何使用实现同样的事情数据表


解决方案

  DataTable的DT =新的DataTable();SqlDataAdapter的适配器=新SqlDataAdapter的(CMD);adapter.Fill(DT);的foreach(在dt.Rows的DataRow行)
{
    TextBox1.Text =行[的ImagePath]的ToString()。
}

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

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

When I am using DataReader I do it this way:

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

How can I achieve the same thing using DataTable?

解决方案

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.

这篇关于如何通过一个DataTable迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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