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

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

托管调试助手“FatalExecutionEngineError”在“C:\Users\admin\Documents\Visual Studio 2015\Projects\Testing\Testing\bin\Debug\Testing.vshost.exe”中检测到问题。

附加信息:运行时遇到致命错误。错误地址为0x5845e24f,线程为0x2278。错误代码为0xc0000005。此错误可能是CLR或用户代码的不安全或不可验证部分中的漏洞。此错误的常见来源包括用于COM互操作或PInvoke的用户封送错误,可能会破坏堆栈。

错误发生在下列代码行:

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

数据库中Id和Value的数据类型是:

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

表格中的总记录数:3066700

我的列表在抛出异常后已经填满了 322283 条记录。

根据这个链接,它说这是编译器的一个错误。

我无法理解问题所在,不知道我做错了什么。

有人能帮我解决吗?

代码:

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方法时,它会抛出错误。

使用datatable时可以正常工作

代码:

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

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

Unable to cast object of type 'System.Int32' to type 'System.String'
1个回答

2

我猜问题出在数据上,因为当我用900000测试数据时没有问题,但是当我包含所有数据时就会出现错误(无法将类型为'System.Int32'的对象转换为类型'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)    
    };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接