在调用 Read() 之前获取访问字段的无效尝试,但在 C# 中调用了 Read()

本文关键字:Read 调用 但在 字段 获取 访问 无效 | 更新日期: 2023-09-27 18:35:09

好的,我正在尝试使用 MySQL 连接器 6.9.5 从 MySQL 使用 C# 获取数据,并收到以下错误消息:

Error: MySql.Data.MySqlClient.MySqlException: Invalid attempt to access a field before callingRead()
at MySql.Data.MySqlClient.ResultSet.get_Item(Int32 index)
at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index, Boolean checkNull)
at MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32 i)
at PayrollManagementSystem.ConnectorDB.login(String username, String password) in C:'Users'Scarlet'Documents'Visual Studio 2008'Projects'PayrollManagementSystem'PayrollManagementSystem'ConnectorDB.cs:line 41

这意味着我在调用 Read() 方法之前尝试访问一个字段,但我想我已经这样做了,这是我的代码:

public ModelUser login(String username, String password)
    {
        Console.WriteLine("username asal = " + username);
        ModelUser val = null ;
        connection.ConnectionString = connectionString;
        try
        {
            command = new MySqlCommand( "SELECT user.id_data_user, user.username, user.password FROM user WHERE username = '" + username + "'" +
                " AND password = '" + password + "'", connection);              
            MySqlDataReader reader;
            connection.Open();
            reader = command.ExecuteReader();
            val = new ModelUser();
            reader.Read(); == this is the Read() method was called
            val.IDDataUser = reader.GetString(0); === error at this line
            val.Username = reader.GetString(1);
            val.Password = reader.GetString(2);

            Console.WriteLine("data_user di conDb: " + val.IDDataUser);
            connection.Close();
        }
        catch (MySqlException ex)
        {
            Console.WriteLine("Error: {0}", ex.ToString());
        }
        return val;
    }

我不知道我的代码出了什么问题,这是我第一次尝试 C#,感谢您对我的问题的任何回复

更新:

我像这样更改了我的代码

   if (reader.HasRows)
            {
                while (reader.Read())
                {
                    val.IDDataUser = reader.GetString(0);
                    val.Username = reader.GetString(1);
                    val.Password = reader.GetString(2);
                }
            }
            else
            {
                Console.WriteLine("Holy cow,she's got no rows!");
            }

我的读者没有行。 就像它无法从数据库中获取任何数据一样。 这就是日志的样子:

'PayrollManagementSystem.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:'Windows'assembly'GAC_MSIL'System.Configuration'2.0.0.0__b03f5f7f11d50a3a'System.Configuration.dl 
', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'PayrollManagementSystem.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:'Windows'assembly'GAC_64'System.EnterpriseServices'2.0.0.0__b03f5f7f11d50a3a'System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Holy cow,she's got no rows!
username: 
data user: 
id : 
The thread '<No Name>' (0x13f0) has exited with code 0 (0x0).

在调用 Read() 之前获取访问字段的无效尝试,但在 C# 中调用了 Read()

您需要检查reader是否读取一行

使用数据读取器的最佳方法是

if(reader.HasRows)
  {
   while(reader.Read()) // == this is the Read() method was called
       {
        val.IDDataUser = reader.GetString(0); === error at this line
        val.Username = reader.GetString(1);
        val.Password = reader.GetString(2);
        Console.WriteLine("data_user di conDb: " + val.IDDataUser);
       }
   }