我想获取用户名和用户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/