c# - 选择 Auto_Increment 值导致 MySql 异常 C#

标签 c# mysql mysql-connector

我想获取用户名和用户ID,但是当我执行命令时 我收到异常:

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: startIndex"

数据库具有以下项目:UserID(AutoIncrement Int(9)(PK)) 和 UserName(Varchar(20))

代码:

using (MySqlConnection connection = new MySqlConnection(DBConnection.ConnectionString))
{
connection.Open();
MySqlCommand getUser = new MySqlCommand("SELECT * FROM User Where UserID = '42'", connection);
MySqlDataReader reader = getUser.ExecuteReader();
if (reader.HasRows){

The Exception is thrown at "ExecuteReader".

队列

“Select* From UserWhere UserID ='42'”正在执行,服务器上没有任何问题。

我的问题是:

为什么我会遇到索引超出范围异常?

编辑:

我也只尝试了查询

SELECT * FROM User Where User

但我也遇到了同样的异常。

我可以插入到表中,并且可以获得最后的插入 ID,但是当我尝试使用 Select * From XY 对每个数据库进行操作时,我得到了相同的异常。所有表都带有 innobDB,并且都至少有 1 个主键或外键。

编辑2:

如果我只选择用户名,则查询正在运行:

从用户中选择用户名

但是当我尝试获取 UserID 时

从用户中选择用户ID

我得到异常:

{"Destination array is not long enough to copy all the items in the collection. Check array index and length."} System.Exception {System.ArgumentException}

最佳答案

尝试不带引号的 42

"SELECT * FROM User Where UserID = 42"

关于c# - 选择 Auto_Increment 值导致 MySql 异常 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36230747/

相关文章:

c# - 使用 web.config 阻止 IIS 7.5 显示默认错误页面

c# - 这样的 java 代码在 C# 中看起来如何?

c# - sgen.exe 的命令行太长

php - 多个sql查询运行问题

php - 从Mysql获取rowid

mysql jdbc 与 SSL 的连接在 tls 握手级别失败

c# - pinvoke将ascii字符数组返回给c#

php - 小哈娜 ORM : How to query for a timestamp that's calculated based on a field in the DB

java - mysql连接器字符集结果不支持utf8mb4

python - mysql-connector python 'IN' 运算符存储为列表