限制使用 TableAdapter(类型化数据集)返回的行数

发布于 2024-08-10 20:32:28 字数 241 浏览 6 评论 0原文

有谁知道使用类型化 TableAdapter 时限制返回行数的最佳方法?选项似乎是:

  1. 将“top X”作为参数传递(直到我们使用 SQLS2008 才能执行此操作
  2. 发出动态 SQL 语句:set rowcount X 在调用我的 tableadapter 方法之前,然后 set rowcount 0 之后

非常感谢。

Does anyone know the best way to limit the number of rows returned when using typed TableAdapters? Options appear to be:

  1. Pass "top X" as a parameter (can't do this until we're on SQLS2008
  2. Issue a dynamic SQL statement: set rowcount X before calling my tableadapter method, then set rowcount 0 aftwards.
  3. Something else I haven't thought of :).

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放飞的风筝 2024-08-17 20:32:29

这对我来说效果很好。

SELECT DISTINCT TOP (@count) FLD1, FLD2 FROM mytable WITH (nolock)

该参数与 TableAdapter 中的任何其他参数一起生成。

This works fine for me.

SELECT DISTINCT TOP (@count) FLD1, FLD2 FROM mytable WITH (nolock)

The parameter is generated along with any others in the TableAdapter.

很酷又爱笑 2024-08-17 20:32:29

我使用参数化存储过程来实现这一点。

create procedure dbo.spFoo 
    @NoOfRows int = 200

AS


declare @sql varchar(2000)
select @sql =
'select top ' + Cast(@NoOfRows as varchar) + ' * FROM   Foo'

exec(@sql)

I achieve this using parameterized stored procedure.

create procedure dbo.spFoo 
    @NoOfRows int = 200

AS


declare @sql varchar(2000)
select @sql =
'select top ' + Cast(@NoOfRows as varchar) + ' * FROM   Foo'

exec(@sql)

谁的年少不轻狂 2024-08-17 20:32:29

我找到了更好的方法。

您无法参数化“TOP”值,但可以参数化“SET ROWCOUNT X”值。

因此,这是有效的:

create procedure test_SelectTopFromTable (@rowCount int) as
begin
    set rowcount @rowCount
    select * from table
    set rowcount 0
end

exec test_SelectTopFromTable 100

但是我确实需要处理发生的异常,这将阻止 set rowcount 0 语句运行。我会继续挖掘。

更新
我的老板告诉我如何让它发挥作用(而且它太简单了,我有点尴尬),但我会让他发布他的答案,而不是让它看起来像是我自己找到的。 :)

I have found a better way.

You can't parameterise the "TOP" value, but you can parameterise the "SET ROWCOUNT X" value.

Therefore, this works:

create procedure test_SelectTopFromTable (@rowCount int) as
begin
    set rowcount @rowCount
    select * from table
    set rowcount 0
end

exec test_SelectTopFromTable 100

However I do need to deal with exceptions happening, which would prevent the set rowcount 0 statement from running. I'll keep digging.

UPDATE
My boss told me the way to make it work (and it's so simple I'm a bit embarrassed) but I'm going to get him to post his answer rather than make it look like I found it myself. :)

TableadApter 使用 SqlDataAdapter,它已经具有此功能

操作:

单击 Fill()FillByXXX() 方法

myTradesTableAdapter.Fill(...)

,然后按 F12 - 它会将您带到其源代码 由设计师生成。
复制此方法。

转到数据集设计器,单击表适配器并按F7 - 它将为TableAdapters命名空间创建/打开代码

在这里粘贴 Fill() 方法修改它,如下例所示:

namespace MyApp.DsMyDataTableAdapters {

public partial class MyTradesTableAdapter
{
    //// copy-paste method from generated Fill()
    //// and make new name: Fill() ==> FillTop()
    //// use the same params and add new one: int topN
    public virtual int FillTop(             //
         DsMyData.MyTradesDataTable dataTable,
         int someParameterId,
         // ...
         int topN)  // add new param
    {
        // original code:
        this.Adapter.SelectCommand = this.CommandCollection[0];
        this.Adapter.SelectCommand.Parameters[1].Value = someParameterId;

        if ((this.ClearBeforeFill == true))
        {
            dataTable.Clear();
        }


        // modified code
        int returnValue = 0;
        if (topN > 0)                  // validate topN
        {
            // get topN rows
            returnValue = this.Adapter.Fill(0, topN, dataTable);   

        }
        else 
        {
            // get all rows (original code) in case topN = 0 or negative
            returnValue = this.Adapter.Fill(dataTable);
        }
        return returnValue;
    }

    // ....
}

然后您可以使用它

int somePrm = 123;
myTradesTableAdapter.Fill(ds.myTradesTable, somePrm) // original - get all rows
myTradesTableAdapter.FillTop(ds.myTradesTable, somePrm, 100) // new - get 100 rows

TableadApter use SqlDataAdapter, which already has this feature.

What to do:

Click on Fill() or FillByXXX() method

myTradesTableAdapter.Fill(...)

and press F12 - it will bring you to its source code generated by designer.
Copy this method.

Go to the DataSet designer, click on table adapter and press F7 - it will create/open code for the TableAdapters namespace.

Paste Fill() method here and modify it some like in example below:

namespace MyApp.DsMyDataTableAdapters {

public partial class MyTradesTableAdapter
{
    //// copy-paste method from generated Fill()
    //// and make new name: Fill() ==> FillTop()
    //// use the same params and add new one: int topN
    public virtual int FillTop(             //
         DsMyData.MyTradesDataTable dataTable,
         int someParameterId,
         // ...
         int topN)  // add new param
    {
        // original code:
        this.Adapter.SelectCommand = this.CommandCollection[0];
        this.Adapter.SelectCommand.Parameters[1].Value = someParameterId;

        if ((this.ClearBeforeFill == true))
        {
            dataTable.Clear();
        }


        // modified code
        int returnValue = 0;
        if (topN > 0)                  // validate topN
        {
            // get topN rows
            returnValue = this.Adapter.Fill(0, topN, dataTable);   

        }
        else 
        {
            // get all rows (original code) in case topN = 0 or negative
            returnValue = this.Adapter.Fill(dataTable);
        }
        return returnValue;
    }

    // ....
}

and then you can use it as:

int somePrm = 123;
myTradesTableAdapter.Fill(ds.myTradesTable, somePrm) // original - get all rows
myTradesTableAdapter.FillTop(ds.myTradesTable, somePrm, 100) // new - get 100 rows
柠栀 2024-08-17 20:32:28

您可以使用 TOP n,但必须将其放在括号中,如下所示:

SELECT TOP (100) * FROM ...

您还可以按如下方式对其进行参数化:

DECLARE @count INT
SET @count = 100
SELECT TOP (@count) * FROM field_company

You can use TOP n, but you have to put brackets around it like so:

SELECT TOP (100) * FROM ...

You can also parameterise it as follows:

DECLARE @count INT
SET @count = 100
SELECT TOP (@count) * FROM field_company
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文