c# - 从单行数据表中提取值

标签 c# .net datatable datarow

如何从具有单行的数据表中提取值并分配给 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 单元格的情况下传递空字符串。另外,我可以通过 list 从数据表中提取值吗?

最佳答案

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# - 从单行数据表中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046715/

相关文章:

c# - 如何更改 Short 中的位

C# - 从 PDF 签名获取公钥

c# - RoleManager 类(WriteToLog 方法)已移动或替换?

c# - 跨线程操作无效(How to access WinForm elements from another module events?)

.net - 通过过滤另一个 DataTable 创建一个 DataTable

c# - DataTable 动态计算列

c#属性问题

c# - "the term ' 添加迁移 ' is not recognized as the name of a cmdlet" Visual Studio 2019

.net - 如何将 wiki 标记转换为 RTF?

c# - 需要在 Winform 应用程序的 DataGrid View 中针对每一行放置一个 RadioButton