0
 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 が表示されます

直せません、助けてくれませんか?

4

1 に答える 1

1

ExecuteScalar()を使用できます

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

ExecuteReaderを使用する場合は、最初にリーダーを保存してから、Readを呼び出し、reader.GetString(0);を使用して値をフェッチする必要があります。

于 2012-10-01T15:39:43.747 に答える