简体   繁体   English

如何检查用户名是否存在于数据库的特定列中

[英]How to check if Username exists in specific column of database

I'm using C# with an ASP.Net table. 我正在将C#与ASP.Net表一起使用。 I'm trying to check when a user accesses my web app, that their User ID is listed in the column called UserID in a table that is displayed in the web app. 我正在尝试检查用户何时访问我的Web应用程序,该用户ID是否列在该Web应用程序中显示的表格中名为UserID的列中。 If it is not, then I want to redirect them to an error page. 如果不是,那么我想将它们重定向到错误页面。

I need this to check when the page is loaded so obviously it needs to be in Page_Load . 我需要它来检查何时加载页面,因此很明显它需要位于Page_Load I just don't know how to call the column in my table and to have it check the User ID. 我只是不知道如何在我的表中调用该列并使其检查用户ID。

This is what I have so far: 这是我到目前为止的内容:

protected void Page_Load(object sender, EventArgs e)
{            
    if (!Page.IsPostBack)
    {
        string UserID = HttpContext.Current.Request.LogonUserIdentity.Name.Substring(4);
        SQLUserID();

        if (UserID != //'Whatever values are in the UserID column'//)
        {
            Server.Transfer("ErrorPage.aspx");
        }

        SQLCmd = SqlDataSource1.SelectCommand;
        GridView1.DataBind();
    }
}

The string UserID gives me the User ID of the user. 字符串UserID给我用户的用户ID。 SQLUserID() = SELECT UserId FROM Info. I know the way I called that isn't correct but I'm not sure how to do it. 我知道我所说的方式不正确,但是我不确定该怎么做。

It can help you: 它可以帮助您:

SqlConnection conn = new SqlConnection(connectionString); 
SqlCommand check_User_Name ("SELECT COUNT(*) FROM tblUser WHERE (UserID=@UserID)",conn);
check_User_Name.Parameters.AddWithValue("@UserID,UserID);
int userExist=(int)check_User_Name.ExecuteScalar();
if(userExist>0)
{
    //Login
}
else
{
   Server.Transfer("ErrorPage.aspx");
}

just write your connection string and enjoy it. 只需编写您的连接字符串即可。

So here is how I got it to work below Page_Load 所以这就是我如何使其在Page_Load下工作

{
string connectionString = "Data Source=###;Initial Catalog=###;Integrated Security=True";
        if (!Page.IsPostBack)                
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();

                using (SqlCommand sqlCommand = new SqlCommand("SELECT COUNT(UserID) from Info WHERE UserID like @UserID", sqlConnection))
                {
                    string UserID = HttpContext.Current.Request.LogonUserIdentity.Name.Substring(4);                        
                    sqlCommand.Parameters.AddWithValue("@UserID", UserID);
                    int userCount = (int)sqlCommand.ExecuteScalar();
                    if (userCount == 0)
                    {
                        Server.Transfer("ErrorPage.aspx");
                    }

                    SQLCmd = SqlDataSource1.SelectCommand;
                    GridView1.DataBind();
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM