//判断满足条件的记录是否存在,程序如下。问有没有比下面程序更简单的办法。
//只判断满足条件的记录是否存在,不读取任何字段内容。程序越简单越好。
string strrec;
string strsql = "select count(*) c1 from b1 where id=4"
SqlCommand coom1 = new SqlCommand(strsql, conn);
try
{
conn.Open();
dr=coom1.ExecuteReader();
if (dr.Read()) 
{
count1 = dr["cc"].ToString();
if (count1=="0")
{
strrec="记录存在。";
}
else
{
strrec="记录不存在。";
}
}
conn.Close();
}
catch(Exception ee)
{
throw ee;
}

解决方案 »

  1.   

    string strrec;
    string strsql = "select id from b1 where id=4"
    SqlCommand coom1 = new SqlCommand(strsql, conn);
    try
    {
    conn.Open();
    dr=coom1.ExecuteReader();
    if (dr.Read()) 
    { strrec="记录存在。";}
    else
                {  strrec="记录不存在。";}
    conn.Close();
    }
    catch(Exception ee)
    {
    throw ee;
    }
      

  2.   

    int count=coom1.ExecuteNonQuery()
      

  3.   

    这个用下面的代码可能比较简单:  string strsql = "select count(*) c1 from b1 where id=4"
    SqlCommand cmd = new SqlCommand(strsql, conn);
       int i = cmd.ExecuteScalar();
                if (i > 0)
                {
                    //数据正确时所执行的语句
                }
                else if (i = 0)
                {
                    //数据不存在时所执行的语句
                }
                else
                { 
                    //数据错误
                }   你试试看这个代码
      

  4.   

    select count(*) from sss