[英]MySql Exception by Selecting Auto_Increment Values C#
I want to get the username and userid, but when i executing the command I get the Exception: 我想获取用户名和用户名,但是当我执行命令时,出现异常:
Index was out of range. 索引超出范围。 Must be non-negative and less than the size of the collection.\\r\\nParameter name: startIndex" 必须为非负数,并且小于集合的大小。\\ r \\ n参数名称:startIndex“
The Database has the Items: UserID(AutoIncrement Int(9)(PK)) and UserName(Varchar(20)) 该数据库具有以下各项:UserID(AutoIncrement Int(9)(PK))和UserName(Varchar(20))
Code: 码:
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". 在“ ExecuteReader”处引发异常。
Queue 队列
"Select* From User Where UserID ='42'" is Executing without any trouble on the Server. 正在执行“从用户ID = '42'的用户中选择*”,而服务器上没有任何问题。
My Question is: 我的问题是:
Why did I get an Index out of Range Exception ? 为什么我的索引超出范围异常?
Edit: 编辑:
I also tried only the query 我也只尝试查询
SELECT * FROM User Where
User
选择*从用户其中User
But I get the same exception. 但是我也有同样的例外。
I can insert into the Table and I can get the Last insert ID but when I try to every Database with Select * From XY I get the same Exception. 我可以插入表中,也可以获取最后插入ID,但是当我尝试使用Select * From XY对每个数据库进行操作时,会得到相同的异常。 All the Tables are with innobDB and the all have at least 1 primary or foreign key. 所有表都带有innobDB,并且所有表至少具有1个主键或外键。
Edit2: 编辑2:
If I only select the UserName the Query is working: 如果仅选择用户名,则查询有效:
Select UserName From User 从用户中选择用户名
But when I try to get the UserID 但是当我尝试获取用户ID时
Select UserID From User 从用户中选择用户ID
I get the Exception: 我得到异常:
{"Destination array is not long enough to copy all the items in the collection. Check array index and length."} System.Exception {System.ArgumentException} {“目标数组不足以复制集合中的所有项目。检查数组索引和长度。”} System.Exception {System.ArgumentException}
尝试42不加引号
"SELECT * FROM User Where UserID = 42"
Maybe this edit of your code can help you. 也许您对代码的这种编辑可以为您提供帮助。
using (MySqlConnection connection = new MySqlConnection(DBConnection.ConnectionString))
{
connection.Open();
MySqlCommand getUser = new MySqlCommand("SELECT * FROM `User` WHERE `User`.`UserID` = 42", connection);
MySqlDataReader reader = getUser.ExecuteReader();
//rest of your code
}
I hope you get the idea. 希望您能明白。
As a wise error spotting procedure I suggest to check if the error is located at sql level. 作为明智的错误发现程序,我建议检查错误是否位于sql级别。
Try to get rid of the where clause simply executing a plain query 尝试简单地执行普通查询来摆脱where子句
SELECT * FROM `User`
Do you notice some change? 你注意到一些变化吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.