winform中sql查询

来源:百度知道 编辑:UC知道 时间:2024/06/03 13:48:58
我要写一个事件!就是输入要查询的信息之后,按下按钮之后!如果有此信息就在表格中显示出来(绑定了数据源),如果没有此信息弹出对话框,我写的这个总是不对!显示出来没问题!关键是弹出对话框不对!请高手帮助下。
private void button8_Click(object sender, EventArgs e)
{

if (this.tbCardNumber.Text == "")
{
MessageBox.Show("请输入要查找的账号信息!", "信息提示");

}
else
{
lookID(this.tbCardNumber.Text.Trim());
}

}

public void lookID(string cardNumber001)
{
string str = @"server=.;user id=sa;pwd=;database=YouZheng";
SqlConnection conn = new SqlConnection(str);
string sql = string.Format("select * from users WHERE name='{0}'".cardNumber001);
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int i = cmd.ExecuteNonQuery();
if (i &

conn.Open();
try
{
if(cmd.ExecuteNonQuery()==1)
MessageBox.Show("成功查找!", "信息提示");
else
MessageBox.Show("没有要你查找账户信息!请先核实账户信息 !", "信息提示");

}
catch(sqlException)
{
MessageBox.Show("没有要你查找账户信息!请先核实账户信息 !", "信息提示");
}
finally
{
MessageBox.Show("没有要你查找账户信息!请先核实账户信息 !", "信息提示");
conn.Close();
}

要大写,C#是区分大小写的
catch(SqlException)
OK啦

-----------------
第二次修改:
把finaplly里面的MessageBox去掉。就实现你想要的
finally
{
conn.Close();
}

当我与数据库打交道时,是这么做的:
sting driUrl="server=.;datatase=YouZheng;uid=sa;pwd=";
sqlconnection con=new sqlconnection(driUrl);
con.open();//可能是数据库打开的位置错了,把你的这句挪上面去
sqlcommand cmd=new Sqlcommand("select * from users where name='"+name+"' and careNumber='"+号码+"'",con)