简体   繁体   中英

.Rows.Count always returns -1 even if record in database already exists?

Basically I want to check, using c#, if there is a username in the database already existing. If there is not-it must be created. Below is the code that is doing exactly that(only the checking part matters in my opinion, but I have added the code for the adding part anyways)

Problem is that no matter what-if it exists or not, it always returns -1

public Boolean checkUser()
        {

            var connection = new MySqlConnection("Server=127.0.0.1;Database=book_library;user=root;password=root2");

            var table = new DataTable();

            connection.Open();

            string checkUsernameQuery = "SELECT * FROM accounts WHERE username = 'usernameInputField.Text'";

            var adapter = new MySqlDataAdapter();

            MySqlCommand command = new MySqlCommand(checkUsernameQuery, connection);

            //command.Parameters.Add("@username", MySqlDbType.VarChar).Value = usernameInputField.Text;

            adapter.SelectCommand = command;
            adapter.Fill(table);

            if (table.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

Here is the adding part(just adding it, but it is not too related with the problem)

 private void registerIconButton_Click(object sender, EventArgs e)
        {
            checkUser();

            var connection = new MySqlConnection("Server=127.0.0.1;Database=book_library;user=root;password=root2");

            connection.Open();

            string insertQuery = "INSERT INTO `accounts` (username, password, email) VALUES ('" + usernameInputField.Text + "','" + passwordInputField.Text + "','" + emailInputField.Text + "')";

            MySqlCommand command = new MySqlCommand(insertQuery, connection);

            command.Parameters.Add("@username", MySqlDbType.VarChar).Value = usernameInputField.Text;
            command.Parameters.Add("@password", MySqlDbType.VarChar).Value = passwordInputField.Text;
            command.Parameters.Add("@email", MySqlDbType.VarChar).Value = emailInputField.Text;

            if (checkUser())
            {
                MessageBox.Show("This username already exists!");
            }
            else
            {

                if (command.ExecuteNonQuery() == 1)
                {

                }
                else
                {
                    MessageBox.Show("ERROR");
                }
            }

            connection.Close();

        }

Connections and Commands are disposable so you should put them inside using blocks to deterministically clean up resources.

If you just want to know if something exists, you can use ExecuteScalar instead of using a heavy-weight adapter.

Then just return the result of the expression != null to get your boolean that indicates if the user is there or not.

using(var connection = new MySqlConnection("Server=127.0.0.1;Database=book_library;user=root;password=root2"))
using(var cmd = connection.CreateCommand())
{
    connection.Open();

    string checkUsernameQuery = "SELECT TOP 1 username FROM accounts WHERE username = @p1";

    cmd.Parameters.Add(new MySqlParameter("@p1", MySqlDbType.VarChar).Value = usernameInputField.Text;

    var user = cmd.ExecuteScalar();

    return user != null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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