gpt4 book ai didi

c# - 使用具有行限制的数据适配器填充数据集

转载 作者:搜寻专家 更新时间:2023-10-30 20:40:35 25 4
gpt4 key购买 nike

我需要修改下面的代码来限制行数。

// 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

我知道有一个 fill 方法需要最大计数。

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

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

非常感谢任何帮助。

最佳答案

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

关于c# - 使用具有行限制的数据适配器填充数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com