【发布时间】:2018-07-12 14:31:49
【问题描述】:
当我尝试填充列表对象时出现以下错误:
托管调试助手“FatalExecutionEngineError”检测到 'C:\Users\admin\Documents\Visual Studio 中的问题 2015\Projects\Testing\Testing\bin\Debug\Testing.vshost.exe'。
附加信息:运行时遇到致命错误。这 错误地址位于线程 0x2278 上的 0x5845e24f。错误 代码是 0xc0000005。此错误可能是 CLR 或 用户代码的不安全或不可验证部分。这个的常见来源 错误包括 COM-interop 或 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
我的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;
}
}
我已经在我的同事 PC 上测试了 List 对象代码,直到 900000 它工作正常,但现在当我处理所有记录时它会抛出错误:
Unable to cast object of type 'System.Int32' to type 'System.String'
【问题讨论】:
标签: c# .net ado.net clr fatal-error