简体   繁体   English

检查可用的用户名不起作用

[英]Checking Username Available Doesn't Work

I have an asp.net web forms application which when a user tabs out of the username field it checks my database to see if its available or not but the issue I am having is that it always seems to fall into the exists even if it doesn't. 我有一个asp.net Web窗体应用程序,当用户从用户名字段中跳出时,它会检查我的数据库以查看其数据库是否可用,但我遇到的问题是,即使它没有,它似乎也总是存在没错

I have watched many videos on it and also read many articles but I can't get it to work at all. 我看了很多视频,也看了很多文章,但是我根本无法使用它。

I have provided all my code below. 我在下面提供了所有代码。

Config 设定档

<add name="PaydayLunchConnectionString1" connectionString="Data Source=********\*******;Initial Catalog=************;Integrated Security=True"
  providerName="System.Data.SqlClient" />

HTML 的HTML

<asp:GridView ID="tblUsers" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlUsers" GridLines="None" Width="15%">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        </Columns>
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#EFF3FB" />
</asp:GridView>
<asp:SqlDataSource ID="SqlUsers" runat="server" ConnectionString="<%$ ConnectionStrings:PaydayLunchConnectionString1 %>" SelectCommand="SELECT [Name] FROM [Users] WHERE [name] != 'Admin'"></asp:SqlDataSource>
<asp:Label ID="removeUserNotExist" runat="server" Text="The user entered does not exist. Please try again." Visible="false" style="color: red"></asp:Label>
<asp:Label ID="removeUserExists" runat="server" Text="The user entered exists." Visible="false" style="color: green"></asp:Label>
<div class="form-group">        
    <asp:Label runat="server" AssociatedControlID="txtRemoveUser" CssClass="col-sm-offset-2 col-sm-3 control-label">Enter Name To Be Removed</asp:Label>
    <div class="col-sm-3">
        <asp:TextBox runat="server" ID="txtRemoveUser" CssClass="form-control" AutoPostBack="true" OnTextChanged="txtRemoveUser_TextChanged" />
    </div>
</div>

Code Behind 背后的代码

using System.Data.SqlClient;
using System.Configuration;
using System.Data;

protected void txtRemoveUser_TextChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtRemoveUser.Text))
    {
        string connection = ConfigurationManager.ConnectionStrings["PaydayLunchConnectionString1"].ConnectionString;
        SqlConnection conn = new SqlConnection(connection);

        conn.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE Name != @Name", conn);
        cmd.Parameters.AddWithValue("@Name", txtRemoveUser.Text);
        SqlDataReader rd = cmd.ExecuteReader();
        if (rd.HasRows)
        {
            removeUserNotExist.Visible = true;
            removeUserExists.Visible = false;
        }
        else
        {
            removeUserNotExist.Visible = false;
            removeUserExists.Visible = true;
        }
    }
}

DB Details 数据库详细信息

Table Name = Users
Columns = ID, Name, Password
Users = Test, Test2

If I enter 'Test' in the field and tab out, I get the correct message (Exists) but if i then enter 'ABC' I still get the 'Exists' message. 如果我在该字段中输入“测试”并跳出,则得到正确的消息(存在),但是如果我随后输入“ ABC”,则仍然得到“存在”消息。

If there is more than 1 user in your database, this query will always produce rows. 如果您的数据库中有多个用户,则此查询将始终产生行。 Hence, your if statement always produces the same result: 因此,您的if语句始终产生相同的结果:

SELECT * FROM Users WHERE Name != @Name

If you want to check if a user name exists, simply check for equality. 如果要检查用户名是否存在,只需检查是否相等。

SELECT * FROM Users WHERE Name = @Name

If that one returns a row, the user name exists. 如果返回一行,则表明用户名存在。 Otherwise it doesn't. 否则就不会。

A better solution would be to use 1 in the select , since that prevents the database to return all row data, a small performance improvement: 更好的解决方案是在select使用1 ,因为这样可以防止数据库返回所有行数据,这对性能有很小的改善:

SELECT 1 dummy FROM Users WHERE Name = @Name

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

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