【问题标题】:How to get a specific column value from a DataTable in c#如何从 C# 中的 DataTable 中获取特定列的值
【发布时间】:2014-08-26 12:22:47
【问题描述】:

c#中如何从DataTable中获取特定列的值

我的代码 C# 有问题。

我需要从 DataTable 中读取特定的列值。

我尝试使用此解决方案没有成功,因为输出是查询中选择的列的名称 (File) 而不是字段数据库的值。

非常感谢您在解决这个问题时给我的任何帮助。

这是我的代码:

public DataTable GridViewBind()
{
    using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {    
        sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";


        using (OdbcDataAdapter command =
            new OdbcDataAdapter(sql1, cn))
        {
            try
            {
                cn.Open();

                dset = new DataSet();
                dset.Clear();
                command.Fill(dset);
                DataTable dt = dset.Tables[0];
                GridView1.DataSource = dt;                    
                GridView1.DataBind();

                Response.Write(dt.Columns[0].ToString());

                return dt;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }

                if (cn != null)
                {
                    cn.Close();
                    cn.Dispose();
                }
            }
        }
    }
}

编辑#1

现在我有错误:

System.IndexOutOfRangeException:位置 0 处没有行。

public DataTable GridViewBind()
{
    using (OdbcConnection cn =
        new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
    {    
        sql1 = " SELECT FILE FROM tbl_A WHERE Id = 1; ";


        using (OdbcDataAdapter command =
            new OdbcDataAdapter(sql1, cn))
        {
            try
            {
                cn.Open();

                dset = new DataSet();
                dset.Clear();
                command.Fill(dset);
                DataTable dt = dset.Tables[0];
                GridView1.DataSource = dt;                    
                GridView1.DataBind();

                string file = dt.Rows[0].Field<string>(0);
                Response.Write(file.ToString());  

                return dt;
            }
            catch (Exception ex)
            {
                throw new ApplicationException("operation failed!", ex);
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }

                if (cn != null)
                {
                    cn.Close();
                    cn.Dispose();
                }
            }
        }
    }
}

【问题讨论】:

  • 问题不清楚。是什么使哪一列的值具体?该表通常包含多行。使用循环并使用row.Field&lt;string&gt;(0) 访问每一行的值。
  • 我需要读取在查询 sql1 中选择的列 File 的值。
  • 也许有帮助: if ( dt.Columns[ 0 ].ColumnName == "columnName" ) { gridview1.DataSource = dt; gridview1.DataBind(); }

标签: c# datatable


【解决方案1】:

该表通常包含多行。使用循环并使用row.Field&lt;string&gt;(0) 访问每一行的值。

foreach(DataRow row in dt.Rows)
{
    string file = row.Field<string>("File");
}

您也可以通过索引访问它:

foreach(DataRow row in dt.Rows)
{
    string file = row.Field<string>(0);
}

如果你期望只有一行,你也可以使用DataRowCollection的索引器:

string file = dt.Rows[0].Field<string>(0); 

由于如果表为空则失败,使用dt.Rows.Count检查是否有行:

if(dt.Rows.Count > 0)
    file = dt.Rows[0].Field<string>(0);

【讨论】:

  • @Hamamelis:我已经在我的回答中的评论中提到了这一点(“如果表格为空则失败......”)。我现在编辑了我的答案以使其更清楚。
  • 我知道为时已晚。 .但是如果我只想要数据表标题,而特定的数据表列标题呢?
【解决方案2】:

这很容易实现。从DataTable,首先我们可以应用Select 函数来仅获取与Id=1 关联的行。
之后,我们可以执行CopyToDataTable() 函数以获取DataTable 格式。
由于我们期望Id=1只有一行,我们可以直接应用Rows[0],然后给出列名,转换为字符串并赋值给file变量。

string file = dt.Select("Id=1").CopyToDataTable().Rows[0]["FILE"].ToString()

请注意,如果您选择字符串值,则需要应用单引号。比如Id是字符串类型,那么你需要像下面这样申请。

Id='1'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多