编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

录入时检测数据是否存在的验证(录入时检测数据是否存在的验证方法)

wxchong 2024-07-16 10:07:43 开源技术 91 ℃ 0 评论

实现:无输入时,提示需要录入;重复时,提示已经存在。

一、数据库知识准备

--查找重复记录,重复记录是根据单个字段(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、表单

<asp:TextBox ID="txtBqid"  lay-verify="title" autocomplete="off" class="layui-input" runat="server" onblur="checkbqid()"></asp:TextBox><span  id="remind"></span>

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("<font color=red>已经存在,请重新输入!</font>"); $("#txtBqid").focus();
                },
                error: function () {
                    $("#remind").html("<font color=green>用户名可用</font>");
                }
            });
        }

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;
            }
        }


Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表