从数据库中获取数据并传递给另一个表单

时间:2012-07-26 08:04:18

标签: c#

我想在label中存储整数值并传递给另一个表单。但是值传递但是它传递system.data.dataset。以下是我的代码。请帮我解决一下这个。

con.Open();
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" + textBox1.Text + "'", con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd1);
DataSet ds = new DataSet();
adapter.Fill(ds, "loki");
label4.Text = ds.ToString();
options frm = new options(label4.Text);
frm.Show();
con.Close(); 

3 个答案:

答案 0 :(得分:2)

您必须在label.Text中传递balance字段值

而不是这个

label4.Text = ds.ToString();

你这样做了

label4.Text = ds.Tables[0].Rows["balance"].ToString();
  

请注意,因为它将作为字符串存储在标签的Text属性中,   所以当你在另一个表格上找回它时   必须使用Int32.TryParse

将其转换为int

答案 1 :(得分:1)

你想得到一个标量'值而不是数据集。

con.Open();
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" 
                        + textBox1.Text + "'", con);
object balance = cmd.ExecuteScalar();
label4.Text = balance.ToString();

options frm = new options(label4.Text);
frm.Show();
con.Close(); 

答案 2 :(得分:0)

尝试

con.Open();
SqlCommand cmd1 = new SqlCommand("select balance from customer where namee='" 
                        + textBox1.Text + "'", con);
label4.Text = cmd1.ExecuteScalar();
相关问题