实现:无输入时,提示需要录入;重复时,提示已经存在。
一、数据库知识准备
--查找重复记录,重复记录是根据单个字段(bqid)来判断 select * from banqi where bqid in (select bqid from banqi group by bqid having COUNT(bqid)>1)--统计指定bqid的记录数 select COUNT(1) from banqi where bqid='333222'
二、代码
1、表单
2、Jquery
function checkbqid() { var bqid = $("#txtBqid").val(); if (bqid == "") { alert("请输入5位数班期ID"); return; } $.ajax({ type: 'get', url: 'bqidcheck.ashx', contentType: "application/json;charset=utf-8", dataType: "text", data: {bqid: bqid}, success: function (data) { $("#remind").html(""); $("#txtBqid").focus(); }, error: function () { $("#remind").html(""); } }); }
3、ashx
string bqid = context.Request.QueryString["bqid"];bool result = new DAL.banqi().Exists(bqid);context.Response.ContentType = "text/plain";context.Response.Write(result);
4、DAL
//检测bqid是否存在 public bool Exists(string bqid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from banqi"); strSql.Append(" where bqid=@bqid"); SqlParameter[] parameters = { new SqlParameter("@bqid",SqlDbType.NVarChar,20)}; parameters[0].Value = bqid; int res= new SqlHelper().ExecuteNonQuery(strSql.ToString(), parameters, CommandType.Text); if (res > 0) { return true; } else { return false; } }