如何检查用户名是否存在于数据库的用户表中?如果存在,如何显示他的信息? [英] How to check if the username existed in the user table of the database or not? And if it is existed, How show his information?

查看:112
本文介绍了如何检查用户名是否存在于数据库的用户表中?如果存在,如何显示他的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Web应用程序的管理员开发一个简单的用户管理系统.我正在为此任务使用ASP.NET向导控件.
我只是放置了一个用于编写用户名的文本框,当管理员单击下一步"按钮时,系统应检查用户名是否存在于数据库中.如果存在,则系统应在管理员的占位符中显示其信息.

我正在为这项任务而苦苦挣扎.我在后台代码中执行了以下操作:

I am trying to develop a simple user management system for the admin of the web application. I am using ASP.NET Wizard Control for this task.
I just put a TextBox for writing the username and when the admin clicks on the Next button, the system should check if the username existed in the database or not. If it is existed, the system should display his information in a placeholder for the admin.

I am struggling with this task. I did the following in the code-behind:

protected void Page_Load(object sender, EventArgs e)
    {

        //Session["Username"] = Username.Text;
        
        //For checking the user
        if (Request.QueryString["Username"] != null)
        {
            String strUserName = Request.QueryString["Username"];

            //Check userName Here
            String strReturnStatus = "false";

            if (CheckUsername(Request.QueryString["Username"]) == true)
            {
                strReturnStatus = "true";
            }
            Response.Clear();
            Response.Write(strReturnStatus);
            Response.End();
        }
    }

    private bool CheckUsername(string username)
    {
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
        string cmdText = "SELECT COUNT(*) FROM employee WHERE Username = @username";

        using(SqlConnection conn = new SqlConnection(connString))
        {
             conn.Open(); // Open DB connection.

             using(SqlCommand cmd = new SqlCommand(cmdText, conn))
             {
                 cmd.Parameters.AddWithValue("@Username", username); // Add the SQL parameter.

                 int count = (int)cmd.ExecuteScalar();

                 // True (> 0) when the username exists, false (= 0) when the username does not exist.
                 return (count > 0);
             }
        }




我试图通过在TextBox中写入任何用户名来测试这一点,但是没有得到任何结果. 如果用户名存在于数据库中,我现在想要显示的是该用户的信息.




I tried to test this by writing any username in the TextBox, but I did not get any result. What I want now is just showing the information of the user if his username existed in the database. How to do that

推荐答案

尝试通过更改命令文本并删除命令参数来进行尝试.
try by changing the command text and removing the command parameters..

private bool CheckUsername(string username)
    {
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";;
        string cmdText = "SELECT COUNT(*) FROM employee WHERE Username = '" + username + '";

        using(SqlConnection conn = new SqlConnection(connString))
        {
             conn.Open(); // Open DB connection.

             using(SqlCommand cmd = new SqlCommand(cmdText, conn))
             {
                 int count = (int)cmd.ExecuteScalar();

                 // True (> 0) when the username exists, false (= 0) when the username does not exist.
                 return (count > 0);
             }
        }
}


这篇关于如何检查用户名是否存在于数据库的用户表中?如果存在,如何显示他的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆