c# - 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;
}
在项目的另一部分中,我将相同的代码与其他变量一起使用,它可以正常工作。
问候
解决方案
推荐阅读
- google-cloud-platform - 如何在 Google 计算实例上运行脚本?
- java - 如何从 InputStream 中准确读取一行(并且没有其他字符)
- php - Laravel ErrorException (E_WARNING) fopen Upload 不起作用 - 仅适用于 S3 上的某些文件
- drag-and-drop - react-admin 中的 ReferenceArrayInput 可以支持拖放排序吗?
- spring - 问候视图和结果视图是否指向同一个对象?
- scrapy-splash - Scrapy Splash 需要永远渲染一个网站
- python - f 字符串格式打印中的浮点数精度
- javascript - ApolloClient: Invariant Violation 1 – 正确设置 apollo 客户端
- postgresql - 我需要一个函数来从表中选择 88 个随机行(不重复)
- keycloak - 我如何使用 keycloak 验证 jbpm 7.24