当前位置: 代码迷 >> Sql Server >> 求sql语句,再给小弟我讲解一下
  详细解决方案

求sql语句,再给小弟我讲解一下

热度:12   发布时间:2016-04-24 09:03:29.0
求sql语句,再给我讲解一下
    public FSTP_ADPMS_GeogIndiInfo GetModelBySql1(Guid ID,string CertNo)
        {
           StringBuilder sbSql = new StringBuilder();
            sbSql.Append(@"SELECT ID,CertNo,RegName,RegPero,CertDate,ProdSca,QCCode,OrganiseUnitID,PicCode,Remark,CreatedBy,CreatedDate,ModifiedBy,ModifiedDate 
FROM [FSTP_ADPMS_GeogIndi] 
WHERE CertNo=@CertNo and  ID<>@ID");

            int n = 0;
            SqlParameter[] parameters = {
new SqlParameter("@CertNo", SqlDbType.VarChar,50),
                      new SqlParameter("@ID", SqlDbType.UniqueIdentifier)                  };
            parameters[n++].Value = CertNo;
            parameters[n++].Value = ID;

            FSTP_ADPMS_GeogIndiInfo model = null;
            //执行查询
            using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.LocalFSTP_ADPMSConnectionString, CommandType.Text, sbSql.ToString(), parameters))
            {
                model = new FSTP_ADPMS_GeogIndiInfo();
                if (rdr.Read())
                {
                    model.ID = (new Guid(rdr[FSTP_ADPMS_GeogIndiInfo.ID_FIELD].ToString()));
                    model.CertNo = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.CERTNO_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.CERTNO_FIELD].ToString();
                    model.RegName = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.REGNAME_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.REGNAME_FIELD].ToString();
                    model.RegPero = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.REGPERO_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.REGPERO_FIELD].ToString();
                    model.CertDate = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.CERTDATE_FIELD]) ? Convert.ToDateTime("1900-1-1") : Convert.ToDateTime(rdr[FSTP_ADPMS_GeogIndiInfo.CERTDATE_FIELD]);
                    model.ProdSca = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.PRODSCA_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.PRODSCA_FIELD].ToString();
                    model.QCCode = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.QCCODE_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.QCCODE_FIELD].ToString();
                    model.OrganiseUnitID = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.ORGANISEUNITID_FIELD]) ? int.MinValue : Convert.ToInt32(rdr[FSTP_ADPMS_GeogIndiInfo.ORGANISEUNITID_FIELD]);
                    model.PicCode = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.PICCODE_FIELD]) ? (new Guid()) : (new Guid(rdr[FSTP_ADPMS_GeogIndiInfo.PICCODE_FIELD].ToString()));
                    model.Remark = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.REMARK_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.REMARK_FIELD].ToString();
                    model.CreatedBy = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.CREATEDBY_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.CREATEDBY_FIELD].ToString();
                    model.CreatedDate = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.CREATEDDATE_FIELD]) ? Convert.ToDateTime("1900-1-1") : Convert.ToDateTime(rdr[FSTP_ADPMS_GeogIndiInfo.CREATEDDATE_FIELD]);
                    model.ModifiedBy = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.MODIFIEDBY_FIELD]) ? string.Empty : rdr[FSTP_ADPMS_GeogIndiInfo.MODIFIEDBY_FIELD].ToString();
                    model.ModifiedDate = Convert.IsDBNull(rdr[FSTP_ADPMS_GeogIndiInfo.MODIFIEDDATE_FIELD]) ? Convert.ToDateTime("1900-1-1") : Convert.ToDateTime(rdr[FSTP_ADPMS_GeogIndiInfo.MODIFIEDDATE_FIELD]);
                }
            }
            return model;
        }

如下代码,我想判断编号是否重复,但是我感觉我做的太复杂了,求一条sql语句,再给我讲一下
------解决思路----------------------
既然是判断是否重复,你还填充数据干嘛~

SELECT TOP 1 1 FROM FROM [FSTP_ADPMS_GeogIndi] WHERE CertNo=@CertNo and  ID<>@ID

就可以了

然后 用ExecuteScalar,判断有没有结果即可
  相关解决方案