問題 - 執行MessageBox.Show("for loop, last row..."); //TEST CODE
行後出現錯誤。可能是什麼原因 ?當我從另一個數據庫獲得結果集時,不存在這樣的問題。 :(我該如何解決這個問題從C#中的DataTable中檢索行時出錯#
碼 -
public void Main()
{
OleDbDataAdapter oleDA = new OleDbDataAdapter();
DataTable dt = new DataTable();
DataColumn col = null;
DataRow row = null;
string strCols = "";
oleDA.Fill(dt, Dts.Variables["MyResultSet"].Value);
col = dt.Columns["MyColumn"];
int lastIdx = dt.Rows.Count - 1;
MessageBox.Show("int declared, for loop..."); //TEST CODE
//loop upto 2nd last data row
for (int i = 0; i <= lastIdx-1; i++)
{
row = dt.Rows[i];
strCols += row[col.Ordinal].ToString() + ", ";
}
MessageBox.Show("for loop, last row..."); //TEST CODE
row = dt.Rows[lastIdx];
strCols += row[col.Ordinal].ToString(); //!!! I GET ERROR HERE !
MessageBox.Show("strCols");
Dts.TaskResult = (int)ScriptResults.Success;
}
錯誤 -
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: There is no row at position -1.
at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)
at System.Data.RBTree`1.get_Item(Int32 index)
at System.Data.DataRowCollection.get_Item(Int32 index)
at My-Long-Code-Goes-Here.csproj.ScriptMain.Main()
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()
你不必在你的'DataTable' – Habib
好,我知道它的任何行。我的結果集中沒有行,因爲我沒有正確完成一個變量映射。有一次,我解決了這個問題,我的SQL是正確的,我得到了一個結果集,我的代碼沒有錯誤地工作。 – Steam