C# 如何使用数据库中的值设置标签文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15685458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:35:18  来源:igfitidea点击:

How do I set my label's text with a value from the database?

c#asp.netdatabase

提问by Will_G

I got in my database a column "Klantnummer" and
I want that value as my label text.
How do I do that?

我在我的数据库中有一个“Klantnummer”列,
我希望该值作为我的标签文本。
我怎么做?

protected string conS = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Databees.mdf;Integrated Security=True;User Instance=True";
protected SqlConnection con;

protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection(conS);
    try
    {
        con.Open();
        string q = "SELECT * FROM tblKLanten;";
        SqlCommand query = new SqlCommand(q, con);
        SqlDataReader dr = query.ExecuteReader();
        Label1.Text = ; //I want here the value of klantnummer
        con.Close();
    }
    catch (Exception ex)
    {
        Label2.Text = "Error";
    }
}

采纳答案by Zeeshan

you can use:

您可以使用:

label1.Text = dr["Klantnummes"].ToString();

回答by Mike Perrenoud

Well, with the information provided it's not terribly easy to answer, but let's assumeyour structure is like this:

好吧,根据所提供的信息,回答起来并不容易,但让我们假设您的结构是这样的:

CREATE TABLE tblKLanten (
    ID INT,
    Klantnummer VARCHAR(50)
)

you'll need to get the field by the index from the reader like this:

您需要通过阅读器的索引获取字段,如下所示:

Label1.Text = dr.GetString(1);

but you're also going to need to read, so you'll need to issue this statement before setting the label:

但是您还需要阅读,因此您需要在设置标签之前发出以下声明:

bool success = dr.Read();
if (success)
{
    // set the label in here
}

but remember, it's based off of the index of the column in the returned statementand so since you're issuing a SELECT *there's no way for me to know what the index is. In the end, I would recommend making some more changes, so consider the following code:

但请记住,它基于返回语句中列索引,因此由于您发出的是一个SELECT *,我无法知道索引是什么。最后,我建议进行更多更改,因此请考虑以下代码:

protected string conS = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Databees.mdf;Integrated Security=True;User Instance=True";
protected SqlConnection con;

protected void Page_Load(object sender, EventArgs e)
{
    con = new SqlConnection(conS);
    try
    {
        con.Open();
        string q = "SELECT * FROM tblKLanten;";
        SqlCommand query = new SqlCommand(q, con);

        using (SqlDataReader dr = query.ExecuteReader())
        {
            bool success = dr.Read();
            if (success)
            {
                Label1.Text = dr.GetString(1);
            }
        }

        con.Close();
    }
    catch (Exception ex)
    {
        Label2.Text = "Error";
    }
}

the usingstatement there will make sure that the SqlDataReadergets disposed properly.

using那里的声明将确保SqlDataReader正确处理。