从C#中的数据库中获取标签文本

时间:2012-10-01 15:36:40

标签: c#

 private void button5_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='1'", conn);
            conn.Open();
            label1.Text = cmd.ExecuteReader().ToString();
            conn.Close();

            SqlConnection conn1 = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='2'", conn1);
            conn1.Open();
            label2.Text = cmd1.ExecuteReader().ToString();
            conn1.Close();

            SqlConnection conn2 = new SqlConnection("Data Source=MAZI-PC\\PROJECTACC;Initial Catalog=programDB;Integrated Security=True");
            SqlCommand cmd2 = new SqlCommand("select label_sh from label_text where label_form='2' and label_form_labelID='3'", conn2);
            conn2.Open();
            label3.Text = cmd2.ExecuteReader().ToString();
            conn2.Close();
        }

我正在用C#开发一个小项目...使用Visiual Studio 2010 ...我想从数据库中获取标签文本,以便用按钮更改用户界面语言... 我写了这段代码,但SQLDATAREADER

有问题

显示标签文本部分              System.Data.SqlClient.SqlDataReader

我无法解决,你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用ExecuteScalar()

label3.Text = (string) cmd2.ExecuteScalar();

如果你想使用ExecuteReader,你必须先存储阅读器,然后调用Read on it并使用reader.GetString(0);

获取它的值。