C# 怎么利用textbox中的字符为条件查找数据库中数据 将结果赋值给定义的变量在其他textbox显示

我做的是windows应用窗体,是在form2中显示,怎么都显示不出来!
form2的 private void Form2_Load(object sender, EventArgs e)
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();

f2.textBox1.Text = f1.textBox1.Text;

string str = "Data Source=.;Initial Catalog=gkcj;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
//f2.textBox1 = f1.textBox1;
string select = "select zkzh from xsb where zkzh='" + f2.textBox1 .Text + "'";

SqlCommand cmd = new SqlCommand(select, conn);
cmd.ExecuteNonQuery();
conn.Close();
}

form1 中 private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Hide();
Form2 f2 = new Form2();
f2.Show();

第1个回答  2010-12-10
可以直接加在要执行sql语句里面
例如:string sql="select * from abc where ID='"+textbox1.text+"'";
然后再运行,要获取查询结果的话可以使用SqlDataReader来获取,然后将获取的值付给其他的textbox.text
第2个回答  2010-12-10
Form1 f1 = new Form1();
Form2 f2 = new Form2();
f2.textBox1.Text = f1.textBox1.Text;
string str = "Data Source=.;Initial Catalog=gkcj;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
//f2.textBox1 = f1.textBox1;
string select = "select zkzh from xsb where zkzh='" + f2.textBox1 .Text + "'";
SqlCommand cmd = new SqlCommand(select, conn);
SqlDataReader sdr=cmd.ExecuteNonQuery();
if(sdr.Read())
{
f2.textbox1.Text=sdr["字段名"].ToString();
}
conn.Close();

我的原答案可以参考

参考资料:http://zhidao.baidu.com/question/204654889.html

本回答被网友采纳
第3个回答  2010-12-10
把源码贴出来看看
相似回答