各位好!
我用C#+SQL SERVER做了个产品系统
现在要判断某记录是否存在,如果存在则提示;该如何实现啊?
判断的字段是ProductId,buyer
变量为ID,username
如果记录中存在productid=ID and buyer=username
则表示已存在。用C#怎么写啊?请大侠们详细指点一下,谢谢!

解决方案 »

  1.   

    wappibz,我不太懂如何实现啊?
    可以给个样例代码吗?非常感谢!
      

  2.   


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString);
            // Connect to the SQL database using a SQL SELECT query 
            // 变量为ID,username把下面sql改成变量
            
            SqlDataAdapter cmd= new SqlDataAdapter("select count(*) from tb_goods where productid=ID and buyer=username ", conn);
            
            //两种方法
             int RecordCount=(Int32)cmd.ExecuteScalar()
            //或者    SqlDataReader dr = new SqlDataReader();
            // dr.read(); 
            // int RecordCount=dr(0).ToString();
      

  3.   

    string str="select * from 表 where productid=ID and buyer=username"
    int i=cmd.scarlar()
    if(i>0)
    {存在} 
      

  4.   


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString);
        
            SqlCommand cmd= new SqlCommand ("select count(*) from tb_goods where productid=ID and buyer=username ", conn);
            
            
             int RecordCount=(Int32)cmd.ExecuteScalar()