c# – System.ArgumentException:Object不是ADODB.RecordSet或ADODB.Record

我用下面的代码填写数据表 –

OleDbDataAdapter oleDA = new OleDbDataAdapter();
DataTable dt = new DataTable();
oleDA.Fill(dt, Dts.Variables["My_Result_Set"].Value); 

我收到错误 –

Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Object is not an ADODB.RecordSet or an ADODB.Record.
Parameter name: adodb
   at System.Data.OleDb.OleDbDataAdapter.FillFromADODB(Object data, Object adodb, String srcTable, Boolean multipleResults)
   at System.Data.OleDb.OleDbDataAdapter.Fill(DataTable dataTable, Object ADODBRecordSet)
   at ST_34944nkdfnfkdffk333333.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()

为什么我会收到此错误?我之前使用过完全相同的代码,它从来没有给我任何问题.

解决方法:

在脚本任务中,我创建了一个名为DtUsers的数据表,在数据表中创建了列,为行单元格添加了行和赋值.然后我将这个数据表存储在一个名为activeDirectoryUsers的对象变量中:

Dts.Variables["User::activeDirectoryUsers"].Value = DtUsers;

然后我创建了一个数据流并添加了一个源脚本组件.我试图使用Fill()并收到与OP相同的错误.

OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
da.Fill(dt, Variables.activeDirectoryUsers);  //error here

Object is not an ADODB.RecordSet or an ADODB.Record

我的解决方案有两个方面:

>将DtUsers数据表添加到DataSet,然后将DataSet存储在对象变量中.

DataSet ds = new DataSet();
ds.Tables.Add(DtUsers);
Dts.Variables["User::activeDirectoryUsers"].Value = ds;

>在源脚本组件中,创建一个新的DataSet并将对象变量强制转换为类型DataSet,并将其分配给DataSet:

DataSet ds = (DataSet)Variables.activeDirectoryUsers;
if (ds == null || ds.Tables.Count == 0) return;
DataTable dt = ds.Tables[0];
//do stuff with dt...

This article带我到这个解决方案.

上一篇:ETL算法详解


下一篇:50个CSS技巧