Asp.net 如何从数据集/数据库获取/编码单个数据值

Asp.net 如何从数据集/数据库获取/编码单个数据值,asp.net,Asp.net,这是数据库的示例 petType PetName PetDetails PetPrice ---------------------------------------- 猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫 狗\uuuuuuuuuuuuuuuuuuuuuuuu非\uuuuuuuuuuuuuuuuu\u3

这是数据库的示例


petType PetName PetDetails PetPrice

----------------------------------------

猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫

狗\uuuuuuuuuuuuuuuuuuuuuuuu非\uuuuuuuuuuuuuuuuu\u3000.00


我制定的准则是:

PetStore.aspx

<form id="form1" runat="server">
<div>

    <p>
        <asp:label ID="Label1" runat="server" CssClass="DataWebControlStyle">
           <HeaderStyle CssClass="HeaderStyle" />
           <AlternatingRowStyle CssClass="AlternatingRowStyle" />
        </asp:label>
        &nbsp;</p>
</div>
</form>

所以我做的代码只显示数据库中的所有数据。。。如果我只想显示单个值,比如“kitty”,我应该怎么做?不是显示kitty的数据,而是只显示我想在页面上的“label1.text”上显示的单词“kitty”。。谢谢

您需要使用
ExecuteScalar
获取单个值。检查下面的链接。它将从结果集中返回第一行的第一列

此外,请检查以下URL,以将值从executescalar分配到标签:


另外,您的示例令人困惑,因为示例中有
gridview
,说明中有
label1.text
。我在这里假设您将有一个
标签

您需要在绑定字段中指定的列 乙二醇


并使AutoGenerateColumns=“false”


对于只从数据库中获取一条记录或从数据库中逐个获取记录,“数据读取器”是一个很好的方法。查看下面的网站,您可以对数据读取器有清晰的了解

例如:

  SqlConnection connection=New SqlConnection("your connection string");
    SqlCommand command = new SqlCommand(
      "SELECT  PetName  FROM TableName;",
      connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            Label.Text=reader[0].ToString();
        }
    }
    else
    {
        Console.WriteLine("No rows found.");
    }
    reader.Close();

如果需要
值:

public DataSet GetPetDetails()
{
    DataSet ds = new DataSet();
    string connectString = constr.GetConString();
    using( var con = new OleDbConnection(connectString))
    using( var cmd = new OleDbCommand("select * from PetMaster", con))
    {
        con.Open();
        using(var adp = new OleDbDataAdapter(cmd))
        {
            adp.Fill(ds);
        }
    }
    return ds;
}
要使用逗号分隔显示
'PetName'
所有列值

protected void Bind()
    {
        DataSet ds = new DataSet();
        ds = GetPetDetails();
        if (ds != null)
        {
            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow dr in table.Rows)
                {
                    label1.text += dr["PetName"].ToString()+",";

                }
            }
        }
    }

protected void Page_Load(object sender, EventArgs e)
{
    Bind();
}
单列和单行值:(仅单词Kitty)

注意:考虑到表中的上述样本数据。”“0”是数据集中的行号,PetName是列。

如果有任何问题,请告诉我?
希望这能帮助你……!:)

实际上。。这不是gridview。。这是一个标签。。抱歉^^您只需要将该值分配给label1.Text。我已使用指向您正在查找的确切场景的链接更新了答案。您的代码毫无意义<代码>标签
没有
标题样式
属性。也许您正在考虑一个
数据网格
?感谢您的回复^^^^但重点仍然是。。我想知道可以从数据库中获取单词/值“kitty”到我的label.text的脚本/代码^^如果我只为petName做新的查询,我想这一定是我不需要从数据库中做新查询的方式谢谢你的回复^^^但我的观点仍然是。。我想知道可以将单词/值“kitty”从数据库获取到我的label.text的脚本/代码^^如果我只为petName进行新查询,我想这一定是我不需要从数据库进行新查询的方式谢谢。您可以使用“where”获得特定的行访问权限子句,否则请使用以下代码-1:这不是使用块的
方法。调用方必须负责
SqlConnection
。同样,您的代码需要使用
块将
SqlCommand
SqlDataReader
放入它们自己的
  SqlConnection connection=New SqlConnection("your connection string");
    SqlCommand command = new SqlCommand(
      "SELECT  PetName  FROM TableName;",
      connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            Label.Text=reader[0].ToString();
        }
    }
    else
    {
        Console.WriteLine("No rows found.");
    }
    reader.Close();
public DataSet GetPetDetails()
{
    DataSet ds = new DataSet();
    string connectString = constr.GetConString();
    using( var con = new OleDbConnection(connectString))
    using( var cmd = new OleDbCommand("select * from PetMaster", con))
    {
        con.Open();
        using(var adp = new OleDbDataAdapter(cmd))
        {
            adp.Fill(ds);
        }
    }
    return ds;
}
protected void Bind()
    {
        DataSet ds = new DataSet();
        ds = GetPetDetails();
        if (ds != null)
        {
            foreach (DataTable table in ds.Tables)
            {
                foreach (DataRow dr in table.Rows)
                {
                    label1.text += dr["PetName"].ToString()+",";

                }
            }
        }
    }

protected void Page_Load(object sender, EventArgs e)
{
    Bind();
}
label1.text=ds.Tables[0].Rows[0]["PetName"].ToString();