【发布时间】:2026-01-01 00:25:02
【问题描述】:
如果我找到解决方案,我会感到惊讶,因为它非常具体和模糊,但我想我会尝试。我会尽量提供尽可能多的信息,因为我一直在寻找答案。
我正在用 C# 构建一个实用程序,它从 i-series/AS400 上的库中的文件中复制记录,并构建一个加密文本文件,其中 AS400 中的每条记录作为逗号分隔的字符串。在文件中,它将具有文件名、fieldvalue1、fieldvalue2、fieldvalue3 等值。然后,我将该文本文件带到另一台 PC,并运行一个 C# 实用程序,该实用程序将该记录复制到另一台 i 系列机器上的库中的相同文件名中。不幸的是,在某些情况下,我收到了数组异常的外部边界,但我无法确定原因。在异常之前的记录中,记录看起来几乎相同并且工作正常。简而言之,我的代码如下。我通常不会放弃,但我不指望能解决这个问题。如果有人这样做,我今晚可能会唱卡拉OK。
// Select records from AS400 file and write them to text file
Recordset rs = new Recordset();
sqlQuery = "SELECT * FROM " + dataLibrary + "." + fileName;
try
{
rs.Open(sqlQuery, con);
while (!rs.EOF)
{
int[] fieldLengths;
fieldLengths = new int[rs.Fields.Count];
String[] fieldValues;
fieldValues = new String[rs.Fields.Count];
String fullString = "";
for (i = 0; i < rs.Fields.Count; i++)
{
fieldLengths[i] += rs.Fields[i].DefinedSize;
fieldValues[i] += rs.Fields[i].Value;
}
fullString = fileName + "," + String.Join(",", fieldValues);
fullString = Functions.EncryptString(fullString);
File.AppendAllText(savefile.FileName, fullString + Environment.NewLine);
rs.MoveNext();
}
}
catch (Exception ex)
{
}
cmd.Dispose();
// This gives me a text file of filename, fieldvalue1, fieldvalue2, etc...
// Next, I take the file to another system and run this process:
while ((myString = inputFile.ReadLine()) != null)
{
int stringLength = myString.Length;
String[] valuesArray = myString.Split(',');
for (i = 0; i < valuesArray.Length; i++)
{
if (i == 0)
{
fileName = valuesArray[0];
// Create file if it doesn't exist already
createPhysicalFile(newLibrary, fileName);
SQLStatement = "INSERT INTO " + newLibrary + "." + fileName + "VALUES(";
}
else
{
if (i == valuesArray.Length - 1)
{
SQLStatement += "@VAL" + i + ")";
}
else
{
SQLStatement += "@VAL" + i + ", ";
}
}
}
try
{
using (connection)
{
try
{
connection.Open();
}
catch (Exception ex)
{
}
// Create a new SQL command
iDB2Command command = new iDB2Command(SQLStatement, connection);
for (i = 1; i < valuesArray.Length; i++)
{
try
{
command.Parameters.AddWithValue("@VAL" + i, (valuesArray[i]));
}
catch (Exception ex)
{
}
}
// Just split the array into a string to visually check
// differences in the records
String arraySplit = ConvertStringArrayToString(valuesArray);
// The query gets executed here. The command looks something
// like:
// INSERT INTO LIBNAME.FILENAME VALUES(@VAL!, @VAL2, @VAL3, @VAL4)
// There are actually 320 fields in the file I'm having a problem with,
// so it's possible I'm overlooking something. I have narrowed it down to
// field # 316 when the exception occurs, but in both cases
// field 316 is blanks (when it works and when it doesn't).
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
// Here I get the exception out of bounds error in MSCORLIB.DLL.
// Some records are added fine, while others cause this exception.
// I cannot visibly tell any major differences, nor do I see any
// errors in the AS400 job log or anything in C# that would lead me
// down a certain path.
String error = ex.Message;
}
}
【问题讨论】:
-
您收到错误消息的原因只有一个,与 MSCORLIB 无关(这正是引发异常的地方)。它在您的代码中,这是因为您正在读取一个数组的边界之外,这意味着您需要使用调试器来确定代码的哪一部分超出了
fieldLengths、@987654323 的边界@、rs.Fields或valuesArray。我们无法为您执行此操作,因为我们没有来自rs.Open的查询结果可用于执行此操作。引发该异常的其他原因为零。 -
为什么不启动调试器并单步执行此代码?我们无法为您执行此操作,因为这是发现您的代码失败的确切位置的最简单方法。
-
我猜你没有检查数组长度是否非零。您是否使用调试器验证了哪一行引发了异常?逐行遍历它,直到你击中它。
-
当您在其中时,您不妨抽出
fieldLengths变量,因为它从未使用过。或者问题是它应该在某处使用而它不是? -
为什么你的数组在某些地方是 0 索引,但在导致你悲伤的循环中是 1 索引?
标签: c# sql db2 ibm-midrange