我有以下代码用于构建固定长度的文本文件。
TestFile.WriteLine(string.Format(
//Formats with negative numbers are left justified. e.g. {1,-10}
"{0,50}" + //SPACER 50
"{1,-10}" + //NCSBN ID
"{2,10}" + //SPACER 10
"{3,-50}" + //LAST NAME
"{4,-30}" + //FIRST NAME
"{5,-30}" + //MIDDLE NAME
"{6,90}" + //SPACER 90
"{7,-8}" + //DOB
"{8,-1}" + //GENDER
"{9,-9}" + //SOCIAL SECURITY NUMBER
"{10,41}" + //SPACER 41
"{11,-60}" + //ADDRESS 1
"{12,-60}", //ADDRESS 2
DbReader.GetString(0),DbReader.GetString(1),DbReader.GetString(2),
DbReader.GetString(3), DbReader.GetString(4),DbReader.GetString(5),
DbReader.GetString(6), DbReader.GetString(7), DbReader.GetString(8),
DbReader.GetString(9), DbReader.GetString(10), DbReader.GetString(11),
DbReader.GetString(12)));
一切都很好,直到我添加第 12 个索引然后我收到以下错误:
System.IndexOutOfRangeException was unhandled
Message="Index was outside the bounds of the array."
Source="System.Data"
StackTrace:
at System.Data.Odbc.DbCache.AccessIndex(Int32 i)
at System.Data.Odbc.OdbcDataReader.internalGetString(Int32 i)
at System.Data.Odbc.OdbcDataReader.GetString(Int32 i)
at BuildFile_EXE.Program.Main(String[] args) in G:\IT Development\SSIS\Reports\NursysAPRN_ToNatCouncil\BuildFile_EXE\BuildFile_EXE\Program.cs:line 69
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
string.format 是否有 11 个索引限制,还是我遗漏了一些明显的东西?
谢谢。