asp.net注册页面如何验证用户名是否在数据库存在?求代码

2024-11-19 12:37:32
推荐回答(4个)
回答1:

  您好:代码如下

1.服务器控件后台方法:
        
        //点击注册按钮
        protected void ZhuCe_Click(object sender, EventArgs e)
        {
            string sql1 = "select * from [ChangDiGuanLi].[dbo].[yonghu] where yonghu_name='" + yonghuname.Value + "'";
            if (SqlHelper.ExecuteDataSet(SqlHelper.ECB_Platform, CommandType.Text, sql1, null).Tables[0].Rows.Count > 0)
            {
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
            }
            else
            {
                string sql = "INSERT INTO [ChangDiGuanLi].[dbo].[yonghu] VALUES ('" + yonghuname.Value + "','" + yonghumima.Value + "','" + yonghuclass.Value + "','" + yonghumobile.Value + "',1,getDate())";
                int num = SqlHelper.ExecuteNonQuery(SqlHelper.ECB_Platform, CommandType.Text, sql, null);
                if (num == 1)
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
                }
                else
                {
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
                }
            }
        }
        
2.AJAX方法:
        
                $(".zhuce").live("click", function () {
                if (confirm("确定要注册吗")) {
                    $.ajax({
                        type: "POST",
                        contentType: "application/json",
                        url: "register.aspx/register",
                        async: true,
                        data: "{username:" + $(this).attr("username") + ",pwd:" + $(this).attr("pwd") + "}",
                        dataType: "json",
                        success: function (result) {
                            if (result.d > 1) {
                                alert('注册成功');
                            } else {
                                alert('注册失败');
                            }
                        }
                    });
                }
                else {
                    return false;
                }
            });

  

回答2:

你是想直接在页面就查询数据库??

建议你不要这么做

ajax的话

                 var userName = $('#txtName').val();
                var userPassword = $('#txtPwd').val();
                var rememberMe = document.getElementById("rememberMe").checked;
                $.ajax({
                    type: "POST",
                    contentType: "application/json;charset=utf-8",
                    url: "数据提交的url",
                    data: "{username:'" + userName + "',password:'" + userPassword + "',strIsLongLogin:'" + rememberMe + "'}",
                    dataType: 'json',
                    success: function (result) {
                    if(result.d=="存在"){alert("登陆成功")}
                    else {alert("用户不存在")}
                   }
                    });

后台:

查询数据库 select * from user where uname="+userName+" and pwd="+userPwd+"

看返回几行数据,如果为0 无用户,如果为>0用户存在

查询数据库的代码你可以百度下 我就不在这写了,如果不是这钟方式  你再问我

回答3:

SQL:
IF EXISTS(SELECT 1 FROM [用户表] WHERE [用户名] = [传入的用户名])
RAISERROR('当前注册用户名已存在!', 16, 1)

回答4:

您好:代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

1.服务器控件后台方法:

//点击注册按钮
protected void ZhuCe_Click(object sender, EventArgs e)
{
string sql1 = "select * from [ChangDiGuanLi].[dbo].[yonghu] where yonghu_name='" + yonghuname.Value + "'";
if (SqlHelper.ExecuteDataSet(SqlHelper.ECB_Platform, CommandType.Text, sql1, null).Tables[0].Rows.Count > 0)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
}
else
{
string sql = "INSERT INTO [ChangDiGuanLi].[dbo].[yonghu] VALUES ('" + yonghuname.Value + "','" + yonghumima.Value + "','" + yonghuclass.Value + "','" + yonghumobile.Value + "',1,getDate())";
int num = SqlHelper.ExecuteNonQuery(SqlHelper.ECB_Platform, CommandType.Text, sql, null);
if (num == 1)
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
}
else
{
ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "");
}
}
}

2.AJAX方法:

$(".zhuce").live("click", function () {
if (confirm("确定要注册吗")) {
$.ajax({
type: "POST",
contentType: "application/json",
url: "register.aspx/register",
async: true,
data: "{username:" + $(this).attr("username") + ",pwd:" + $(this).attr("pwd") + "}",
dataType: "json",
success: function (result) {
if (result.d > 1) {
alert('注册成功');
} else {
alert('注册失败');
}
}
});
}
else {
return false;
}
});