【问题标题】:Filling dataset with dataadapter with row limit使用具有行限制的数据适配器填充数据集
【发布时间】:2014-03-23 03:38:23
【问题描述】:

我需要修改以下代码,限制行数。

// create the connection
OracleConnection conn = new OracleConnection("Data Source=oracledb;
    User Id=UserID;Password=Password;");

// create the command for the stored procedure
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT_JOB_HISTORY.GetJobHistoryByEmployeeId";
cmd.CommandType = CommandType.StoredProcedure;

// add the parameters for the stored procedure including the REF CURSOR
// to retrieve the result set
cmd.Parameters.Add("p_employee_id", OracleType.Number).Value = 101;
cmd.Parameters.Add("cur_JobHistory", OracleType.Cursor).Direction =
    ParameterDirection.Output;

// createt the DataAdapter from the command and use it to fill the
// DataSet
OracleDataAdapter da = new OracleDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);//Here is where I need to limit the rows

我知道有一种填充方法需要最大计数。

public int Fill( DataSet dataSet, int startRecord, int maxRecords, string srcTable )

但是,我不知道应该将什么传递给 srcTable。我的存储过程有一个 REF_CURSOR (OUT TYPES.REF_CURSOR)。

非常感谢任何帮助。

【问题讨论】:

    标签: c# .net database dataset dataadapter


    【解决方案1】:

    srcTable 参数是DataSet 对象中DataTable 的名称。

    编辑:

    DataSet 对象会在您调用Fill 时自动添加一个表,如果您没有显式创建一个表。默认名称为“表”。我不相信DataSet 对象关心它正在填充什么类型的数据。它仍然会创建DataTable

    在您通过DataAdapter 致电Fill() 之前。将一个空表添加到 DataSet 并命名,以便您能够在 Fill() 方法期间访问它:

    ds.Tables.Add("myTableName");
    

    然后像这样调用适当的重载Fill() 方法:

    da.Fill(ds, 1, 1000, "myTableName");
    

    或者如果您只是使用表的默认名称,或者不确定您创建的表的名称(怀疑):

    da.Fill(ds, 1, 1000, ds.Tables[0].TableName);
    

    具体使用您的示例,它应该如下所示:

    OracleDataAdapter da = new OracleDataAdapter(cmd);
    DataSet ds = new DataSet();
    ds.Tables.Add();
    da.Fill(ds, 1, maxRowCount, ds.Tables[0].TableName);//Here is where I need to limit the rows
    

    【讨论】:

    • 从代码sn-p可以看出,数据集中没有添加表。输出是一个游标。
    • 使用任何数据填充DataSet 会自动创建并添加DataTable。跳过您的Fill,然后查看调试器中设置的Tables 属性。因为您没有明确地删除表,所以可以使用 ds.Tables[0].TableName 示例。
    • 代码为什么不能抛出?您刚刚创建了一个数据集,当时还没有表,您还在尝试将索引 0 传递给函数吗?
    • 就像我说的,0 是DataSet 在您调用fill 时自动创建的表的索引。测试一下看看。
    • 这将我们带到了最初的问题。当游标是输出参数时,数据集中的表的名称是什么?我调试并发现它是“表”。因此,对于单个光标,它必须是 da.Fill(ds, 1, 1000, "Table");您在回答中提到了这一点,但作为表名的示例。如果您编辑答案以反映我们的发现,我可以将其标记为答案。
    猜你喜欢
    • 2014-07-02
    • 2011-09-26
    • 2013-06-13
    • 2011-02-11
    • 2013-12-07
    • 2014-07-03
    • 2015-06-21
    • 2013-11-27
    • 1970-01-01
    相关资源
    最近更新 更多