功能:我是想查数据表中两个字段若与blplant.Text和 lblchemical.Text匹配则不能重复插入记录!
具体代码:
protected void btnsave_Click(object sender, EventArgs e)
    {
        evaluationResultTableAdapter adaper = new evaluationResultTableAdapter();
        var data = adaper.GetDataByplantname(lblplant.Text);
        if (data.Count > 0)
        {
            var plant = data.Single();//返回唯一的一条数据,如果数据为0或者多条,则抛出异常。
            if (plant.chemicalname == lblchemical.Text)
            {
                this.Response.Write("<script>alert('您已经保存!需要更新请单击更新按钮!');</script>");
            }
            else
            {
                this.Response.Write("<script>alert('您保存!');</script>");
             }
}
错误在这里,我想返回多条记录并与lblchemical.Text比较是否相等!帮忙改改代码!头疼吖!var plant = data.Single();//返回唯一的一条数据,如果数据为0或者多条,则抛出异常。
ASP.NET

解决方案 »

  1.   

    select * from table where field1 = '' and field2 = ''
    一条SQL语句不就搞定的事。怎么弄这么复杂
      

  2.   

    如果改成这样:查询到数据库有符合条件的记录了,但是还是能保存,不知道为什么?怎么改吖?
     protected void btnsave_Click(object sender, EventArgs e)
        {        SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\user.mdf;Integrated Security=True;User Instance=True");
            string sql = "";
            sql = "select *  from evaluationResult where  plantname='" + lblplant.Text + "'and  chemicalname='" + lblchemical.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);// 这样就执行了
            if (ds==null)
            {
                          
                    this.Response.Write("<script>alert('您保存!');</script>");        }        else
            {
                this.Response.Write("<script>alert('您保存!');</script>");
                       }    }
      

  3.   

    如果改成这样:就不能保存,不知道为什么?怎么改吖?
    protected void btnsave_Click(object sender, EventArgs e)
        {        SqlConnection cn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\user.mdf;Integrated Security=True;User Instance=True");
            string sql = "";
            sql = "select *  from evaluationResult where  plantname='" + lblplant.Text + "'and  chemicalname='" + lblchemical.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, cn);
            DataSet ds = new DataSet();
            da.Fill(ds);// 这样就执行了
            if (ds==null)
            {
                          
                    this.Response.Write("<script>alert('您保存!');</script>");        }        else
            {
                this.Response.Write("<script>alert('您保存!');</script>");
                       }    }