Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在调用Read()之前获取访问字段的无效尝试,但在C中调用了Read()#_C#_Mysql - Fatal编程技术网

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

C# 在调用Read()之前获取访问字段的无效尝试,但在C中调用了Read()#,c#,mysql,C#,Mysql,好的,我正在尝试使用MySQL connector 6.9.5通过C#从MySQL获取数据,我得到以下错误消息: 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.MySqlDat

好的,我正在尝试使用MySQL connector 6.9.5通过C#从MySQL获取数据,我得到以下错误消息:

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).
'PayrollManagementSystem.vshost.exe'(托管(v2.0.50727)):加载的'C:\Windows\assembly\GAC\u MSIL\System.Configuration\2.0.0\uu b03f5f7f11d50a3a\System.Configuration.dl
,已跳过加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
“PayrollManagementSystem.vshost.exe”(托管(v2.0.50727)):加载的“C:\Windows\assembly\GAC\U 64\System.EnterpriseServices\2.0.0\Uuu b03f5f7f11d50a3a\System.EnterpriseServices.dll”跳过了加载符号。模块已优化,并且调试器选项“仅我的代码”已启用。
天哪,她没有吵架!
用户名:
数据用户:
身份证件:
线程“”(0x13f0)已退出,代码为0(0x0)。

您需要检查
读卡器
是否读取一行

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

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);
       }
   }

查看msdn参考,我还建议/建议更改该查询以利用
参数
将代码放入while循环
while(reader.Read()){}
我尝试过,但读取器没有行,我的查询是对的。@Yusuf1494
如果(reader.HasRows)检查读取器是否有行是的,我知道,我尝试过,它没有行,就像我的阅读器无法从数据库中获取任何数据。等等,现在它有行了,我不知道如何重新启动我的visual studio和MySQL服务器。顺便说一句,谢谢你的帮助