winform c#.net 怎么判断数据库中存在符合查询要求的数据 sql

来源:百度知道 编辑:UC知道 时间:2024/05/26 14:35:30

select * from 表 where 字段='"+条件+"';

string SqlConn=System.Configuration.ConfigurationSettings.AppSettings["ConnString"];
SqlConnection Conn=new SqlConnection(SqlConn);
Conn.Open();
string mysql="select * from db_hetongguanli_y_g";
SqlCommand cm=new SqlCommand (mysql,Conn);
SqlDataReader dr=cm.ExecuteReader ();
while(dr.Read ())
{
if(!this.IsPostBack)
{
this.DropDownList2.Items.Add(new ListItem(dr["hetongid"].ToString()));
}
}

Conn.Close ();
dr.Close();

给一个最简单的!
cmd就是SqlCommand
if (cmd.ExecuteReader())
{
//有
}
else
{
//没有
}

从该表读数据,存入集合,然后判断集合是否为空

前面定义查询语句,创建Command对象,打开数据连接我就不讲了,太多
根据返回值咯
int count=Convert.ToInt32(cmd.ExcuteScalar);//cmd为Sqlcommand对象
if (count>0)
{
//有,给出提示
}else
{
//无,给