2012-10-01 41 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)值;

相关问题