Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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# C MySQLDataReader.AffectedRows=-1_C#_Mysql_Xampp - Fatal编程技术网

C# C MySQLDataReader.AffectedRows=-1

C# C MySQLDataReader.AffectedRows=-1,c#,mysql,xampp,C#,Mysql,Xampp,在我的MySQL控制台中,我可以看到按id从费率订单中选择价格的结果,我得到以下结果: mysql> select Price from rates order by id; +-------+ | Price | +-------+ | 100 | | 120 | | 150 | | 200 | | 350 | | 700 | | 500 | | 700 | | 800 | | 1300 | | 1500 | | 7000 | | 8000 |

在我的MySQL控制台中,我可以看到按id从费率订单中选择价格的结果,我得到以下结果:

mysql> select Price from rates order by id;
+-------+
| Price |
+-------+
| 100   |
| 120   |
| 150   |
| 200   |
| 350   |
| 700   |
| 500   |
| 700   |
| 800   |
| 1300  |
| 1500  |
| 7000  |
| 8000  |
| 15000 |
| 20000 |
+-------+
15 rows in set
但是当我在这个方法中作为string命令运行它时

我正在使用这个连接字符串:datasource=192.168.43.191;数据库=数据库名称;user=用户名;密码=传递单词;在Visual Studio 2015和XAMPP中运行Apache和MySQL。 这是我第一次遇到这个问题。我希望您能提供帮助

当您的查询是插入/更新/删除查询时设置,而不是当您的查询是选择查询时设置。在代码中,您似乎希望使用该属性

    while (reader.Read())
    {
        string[] row = new string[reader.FieldCount];
        for (int i = 0; i < reader.FieldCount; i++)
            row[i] = reader[i].ToString();
        records.Add(row);
    }
您还可以将代码更改为较短的代码

public List<string[]> ExecuteQuery(string command)
{
    List<string[]> records = new List<string[]>();
    using(com = new MySqlCommand(command, con))
    using(reader = com.ExecuteReader())
    {
        while (reader.Read())
        {
            string[] row = new string[reader.FieldCount];
            for (int i = 0; i < reader.FieldCount i++)
                row[i] = reader[i].ToString();
            records.Add(row);
        }
    }
    return records;
}
但是,一般来说,我建议避免使用那些不能以最高效的方式处理应用程序所需的许多不同类型查询的“一刀切”方法

例如,该方法返回一个包含字符串数组的列表,而实际上,您只返回一个不需要数组的列,并且值可能是转换为字符串的小数,并且在使用它们时可能会转换回小数。我们甚至都没有开始谈论日期。您看到这个方法如何在您的所有应用程序中传播其问题了吗

如果您想要一个通用的解决方案,那么选择一个好的ORM,它可以抽象数据库的使用,并返回正确转换为对象实例的数据。检查或但许多其他存在的

AffectedRows属性不应与SELECT语句一起使用,因为它仅在使用INSERT、UPDATE和DELETE语句时才有意义。以下内容可以解决您的问题:

Int32 fields = reader.FieldCount;
List<String[]> records = new List<String[]>()

while (reader.Read())
{
    String[] row = new String[fields];

    for (Int32 i = 0; i < fields; ++i)
        row[i] = reader.GetString(i);

    records.Add(row);
}

在代码中,您在哪里检查reader.AffectedRows?您对这段代码的期望是什么?我正在通过断点检查它,并将鼠标悬停在阅读器上。谢谢!这解决了我的问题~但我也有其他方法来检索单个值,如ExecuteScalar示例和其他类型,而且这不是一个全能的方法,所以不用担心~
public List<string[]> ExecuteQuery(string command)
{
    List<string[]> records = new List<string[]>();
    using(com = new MySqlCommand(command, con))
    using(reader = com.ExecuteReader())
    {
        while (reader.Read())
        {
            string[] row = new string[reader.FieldCount];
            for (int i = 0; i < reader.FieldCount i++)
                row[i] = reader[i].ToString();
            records.Add(row);
        }
    }
    return records;
}
Int32 fields = reader.FieldCount;
List<String[]> records = new List<String[]>()

while (reader.Read())
{
    String[] row = new String[fields];

    for (Int32 i = 0; i < fields; ++i)
        row[i] = reader.GetString(i);

    records.Add(row);
}