2012-10-01 109 views
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的獲取從數據庫中選擇標籤文本在C#

在標籤的文本部分它顯示 System.Data.SqlClient.SqlDataReader

我不能修復,你能幫幫我嗎?

+0

如果您有100個標籤會發生什麼?打開和關閉連接100次? –

+0

是的,你是對的它不是最佳的解決方案...有什麼辦法用一個連接做幾個查詢? –

+0

打開1個連接然後全部讀取 –

回答

1

可以使用ExecuteScalar()

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

,如果你想使用的ExecuteReader你必須先存儲讀卡器,然後調用它讀取把它拿來與reader.GetString(0)值;

相關問題