SqlConnectuion conn=new SqlConnectuion ("xxx");
conn.open();
SqlCommand command=SqlCommand("select count(*) from tablename where filedname=@a and filename2=@b",conn);
command.parameters.add("@a",text1.text);
command.parameters.add("@b",text2.text);
SqlDataReader reader=command.ExecuteReader();
reader.read()
if (reader.getint(0)==0)
{
   command=SqlCommand("insert into ...where filedname=@a and    filename2=@b",conn);
   command.parameters.clear()
   command.parameters.add("@a",text1.text);
   command.parameters.add("@b",text2.text);
   command.executeNo....
}

解决方案 »

  1.   

    private bool check() 
    {
    DataColumn[] Keys=new DataColumn[1];
    Keys[0]=dsCustManage.Tables["CD_Custmer"].Columns[0];
    dsCustManage.Tables["CD_Custmer"].PrimaryKey=Keys;
    DataRow findRow=dsCustManage.Tables["CD_Custmer"].Rows.Find(txtCustCode.Text.Trim());
    if (txtCustCode.Text.Trim()=="" | cmbCustType.Text.Trim()=="")
    {
    MessageBox.Show("请确定客户代码和客户类型是否输入!","新增失败!",MessageBoxButtons.OK);
    return false;
    }
    else
    if (findRow!=null)
    {
    MessageBox.Show("数据库中已经存在:"+txtCustCode.Text.Trim()+"!","新增失败!",MessageBoxButtons.OK);
    return false;
    }
    else
    return true;
    }
    仔细看看这段代码。它解决了你的问题。