尝试填充列表对象时出现致命错误

Fatal error when trying to populate list object

我在尝试填充列表对象时出现以下错误:

Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Users\admin\Documents\Visual Studio 2015\Projects\Testing\Testing\bin\Debug\Testing.vshost.exe'.

Additional information: The runtime has encountered a fatal error. The address of the error was at 0x5845e24f, on thread 0x2278. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

下面这一行抛出错误:

model n = new model
{
     Id = reader.IsDBNull(id) ? null : reader.GetString(id),
     Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)
};

数据库中 ID 和值的数据类型是:

Id: varchar(60)
Value : numeric(31,13)

Table 中的总记录数:3066700

我的 list 在抛出异常后填满了 322283 条记录。

根据这个 Link 它说这是一个编译器错误。

我不明白我做错了什么。

有人可以帮我解决这个问题吗?

代码:

public class model
{
    public string Id { get; set; }
    public decimal Value { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        var sourceData = GetData(SourceConnectionstring,
                                    "select Id,Value from source");

        var targetData = GetData(TargetConnectionstring,
                                    "select Id,Value from target");

        var data = (from s in sourceData
                    join t in targetData on s.Id equals t.Id
                    where s.Value != t.Value
                    select new
                    {
                        srrId = s.Id,
                        srcValue = s.Value,
                        tgtId = t.Id,
                        tgtValue = t.Value
                    }).ToArray();
    }

    public static List<model> GetData(string connectionString, string sqlQuery)
    {
        List<model> m = new List<model>();
        using (var sqlConnection = new SqlConnection(connectionString))
        {
            using (var command = new SqlCommand(sqlQuery, sqlConnection))
            {
                sqlConnection.Open();
                using (var reader = command.ExecuteReader())
                {
                    var id = reader.GetOrdinal("Id");
                    var value = reader.GetOrdinal("Value");
                    int c = 0;
                    while (reader.Read())
                    {
                        model n = new model
                        {
                            Id = reader.IsDBNull(id) ? null : reader.GetString(id),
                            Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)
                        };
                         m.Add(n);
                         c = c + 1;
                      }
                      reader.Close();
                    }
                }
                sqlConnection.Close();
            }
            return m;
        }
    }

更新: 出现奇怪的行为,有时会抛出错误,有时不会。

当我像这样更新查询时,它工作正常:

select top 900000 Id,Value from source

但是当我调试 GetData 方法时它抛出错误。

数据表工作正常

代码:

public static DataTable GetData(string connectionString, string sqlQuery)
        {
            using (SqlConnection sqlConn = new SqlConnection(connectionString))
            using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
            {
                sqlConn.Open();
                DataTable dt = new DataTable();
                dt.Load(cmd.ExecuteReader());
                return dt;
            }
    }

我已经在我同事的电脑上测试了 List 对象代码,直到 900000 它工作正常,但现在当我处理所有记录时它抛出错误:

Unable to cast object of type 'System.Int32' to type 'System.String'

我猜问题出在数据上,因为当我用 900000 测试数据时它工作正常但是当我包含所有数据时它抛出错误(Unable to cast object of type 'System.Int32' to type 'System.String')所以我意识到可能有数据转换有问题因此,由于以下两行之一可能在转换数据时失败:

Id = reader.IsDBNull(id) ? null : reader.GetString(id),
Value = reader.IsDBNull(value) ? 0 : reader.GetDecimal(value)

这就是我设法解决这个问题的方法,现在它也可以很好地处理 3069300 记录:

public class model
    {
        public object Id { get; set; }
        public object Value { get; set; }
    }
    model n = new model
    {
        Id = reader.GetValue(keyColumn),
        Value = reader.GetValue(snapshotValue)    
    };