c# – 从单行的DataTable中提取值

如何从具有单行的数据表中提取值并分配给asp标签.

private void GetUser(string userId)
    {
        dbr.SelectString = "select name, gender,  address, contactno from userInfo where id = = '" + userId + "' --"; // return single row
        DataTable dt = dbr.GetTable();
        //DataRow row = dt.Rows[0];
        // how to retrieve the fields from the data table.
        //lbl_name = name.ToString();
        //lbl_gender = gender.ToString();
        //lbl_contact = contactno.ToString();
    }

我想过使用foreach循环但是数据表只包含单行.如何在NULL单元格的情况下传递空字符串.另外,我可以通过列表从数据表中提取值吗?

解决方法:

private void GetUser(string userId)
{
    dbr.SelectString = "select name, gender,  address, contactno from userInfo where id = = '" + userId + "' --"; // return single row
    DataTable dt = dbr.GetTable();
    if (dt.Rows.Count > 0) 
    {
    DataRow row = dt.Rows[0];

    lbl_name = row["name"].ToString();
    lbl_gender = row["gender"].ToString();
    lbl_contact = row["contactno"].ToString();
    }
}
上一篇:c# – 如何进一步优化此代码?


下一篇:Datarow到CSV行C#