简体   繁体   English

ASP.NET C#不检查数据库中是否存在用户

[英]ASP.NET C# Not checking user exists in database

I create an if else statement to check whether a database exists in DB, but ASP does not check it? 我创建了一个if else语句来检查数据库中是否存在数据库,但是ASP不会检查它?

I think I am doing this wrong. 我想我做错了。

This is my code... 这是我的代码...

 SqlCommand check = new SqlCommand("SELECT Count(*) FROM [Users] WHERE Username='" + @username + "'", Connect);

        int exist = check.ExecuteNonQuery();

        if (exist > 0)
        {
            txtuser.Text = "User already exists";
        } else 
        // Insert the non-existing data to database

Try like tthis... 像这样尝试...

int exist =(int) check.ExecuteScalar()

ExecuteNonQuery() returns the number of rows affected in database.But here you are not updating or inserting so it will return 0 always. ExecuteNonQuery()返回数据库中受影响的行数。但是这里您没有更新或插入,因此它将始终返回0。

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

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