首页 > 解决方案 > C# 当检查用户是否在 mysql 中存在 System.NullReferenceException

问题描述

我已经多次实现了以下代码,但这次它给了我错误 System.NullReferenceException: The object reference was not set to an object instance

这是代码:

    bool userExists = false;
    string existCheck = "SELECT COUNT(*) FROM tablename WHERE name = @name;";
    MySqlCommand cmd = new MySqlCommand(existCheck, connect);
    cmd.Parameters.AddWithValue("@name", username);
    int userCount = Convert.ToInt32(cmd.ExecuteScalar()); <- The error is produced here
    if (userCount > 0)
            {
                userExists = true;

            }

在项目的另一部分中,我将相同的代码与其他变量一起使用,它可以正常工作。

问候

标签: c#mysqldatabase

解决方案


推荐阅读