WebForm 登陆test

本文介绍了一个简单的用户登录验证过程及与数据库的交互方法。使用JavaScript进行前端表单验证,确保用户名和密码不为空。后端通过C#连接SQL Server数据库,并执行参数化查询以验证用户凭据。
    <script>
        window.onload = function () {
            document.getElementById("form1").onsubmit = function () {
                var userName = document.getElementById("username").value;
                var passWord = document.getElementById("pwd").value;
                if (userName != "" && passWord != "") {
                    return true;
                } else {
                    alert("用户名和密码不能为空!!!");
                    return false;
                }
            }
        }
</script>

        string UserName = context.Request["username"];
        string PassWord = context.Request["pwd"];
  //连接数据库
            SqlConnection con = new SqlConnection("server=.;database=test;uid=sa;pwd=123321");
            //执行sql语句
             string sql = "select count(*) from users where username=@UserName and pwd= @PassWord";
            //string sql = "select * from users";
            SqlCommand cmd = new SqlCommand(sql,con);
            cmd.Parameters.AddWithValue("@UserName",UserName);
            cmd.Parameters.AddWithValue("@PassWord",PassWord);
            con.Open();
            int i = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
            if (i==1)
            {
                context.Response.Write("登录成功!!!");
            }
            else
            {
                context.Response.Write("用户名或密码错误!!");
            }

 

转载于:https://www.cnblogs.com/enych/p/8257104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值