我是 C# 的新手,现在我完全沉迷于这个函数。 任何帮助将不胜感激。
我在 mess.Add(firstname);
上遇到了 OutOfMemoryException
我很确定这是因为阵列故障,但我似乎无法让它工作。
谁能指导我正确的方法?
到目前为止,这是我的代码:
public List<string> SelectEmployee(string pkrelation)
{
SDKRecordset inboundSet = IQSDK.CreateRecordset("R_CONTACT", "", "FK_RELATION = " + pkrelation, "");
inboundSet.MoveFirst();
string person = inboundSet.Fields["FK_PERSON"].Value.ToString();
messages.Add(person);
inboundSet.MoveNext();
SDKRecordset inboundSet2 = IQSDK.CreateRecordset("R_PERSON", "", "PK_R_PERSON = " + person, "");
if (inboundSet2 != null && inboundSet2.RecordCount > 0)
{
inboundSet2.MoveFirst();
do
{
string firstname = inboundSet2.Fields["FIRSTNAME"].Value.ToString();
mess.Add(firstname);
inboundSet.MoveNext();
}
while (!inboundSet2.EOF);
return mess;
}
messages.Add("Error, didn't work.");
return messages;// null;
最佳答案
你打错了。您不小心使用了 inboundSet.MoveNext()
,因此您的 inboundSet2.EOF
永远不会设置为 false
,因为您实际上从未遍历过它。这会导致无限循环,最终会触发 OutOfMemoryException
。
do
{
string firstname = inboundSet2.Fields["FIRSTNAME"].Value.ToString();
mess.Add(firstname);
inboundSet.MoveNext(); // This needs to be inboundSet2!
}
while(!inboundSet2.EOF) //EOF never becomes true causing an infinite loop
关于数组上的 C# OutOfMemoryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298073/