在 SQL Server CE 中将 SqlCEResultSet.Seek 与复合键结合使用

发布于 2024-09-11 13:35:49 字数 1194 浏览 3 评论 0原文

有人可以告诉我如何将 SqlCeResultSet.Seek 方法与复合索引一起使用吗?

我正在尝试寻找 orderno+product code

我的 SQL Server CE 表具有以下索引:

CREATE INDEX orderline_idx ON OrderLines (orderno, item)

我的查找代码是

public bool SeekDeliveryLine(string delnote,string item)
{
        bool isFound = false;

        cmd = new SqlCeCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.TableDirect;
        cmd.CommandText = "OrderLines";
        cmd.IndexName = "orderline_idx ";

        try
        {
            //cmd.SetRange(DbRangeOptions.Match, new object[] { delnote }, null);
            deliveryRS = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
            isFound = deliveryRS.Seek(DbSeekOptions.FirstEqual, new object[] { delnote, item });

            if (isFound)
            {
                deliveryRS.Read();
                currentRowData = this.RetrieveRecord();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return isFound;
    }

该代码始终返回第一个匹配的订单行。

预先感谢

保罗

Can someone show me how I can use the SqlCeResultSet.Seek method with a composite index?

I am trying to seek on orderno+product code.

My SQL Server CE table has the following index:

CREATE INDEX orderline_idx ON OrderLines (orderno, item)

My seek code is

public bool SeekDeliveryLine(string delnote,string item)
{
        bool isFound = false;

        cmd = new SqlCeCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.TableDirect;
        cmd.CommandText = "OrderLines";
        cmd.IndexName = "orderline_idx ";

        try
        {
            //cmd.SetRange(DbRangeOptions.Match, new object[] { delnote }, null);
            deliveryRS = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
            isFound = deliveryRS.Seek(DbSeekOptions.FirstEqual, new object[] { delnote, item });

            if (isFound)
            {
                deliveryRS.Read();
                currentRowData = this.RetrieveRecord();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return isFound;
    }

The code always returns the first matching order line.

Thanks in advance

Paul

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

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

发布评论

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

评论(2

十级心震 2024-09-18 13:35:49

查找输入数据数组必须是等于索引字段的对象。您无法查找不在索引中的字段。看起来您在订单号和项目上有一个索引,我认为它是一个整数和一个字符串,但您传递的是“delnote”和项目,它们都是字符串。

The seek input data array must be objects that equate to the indexed fields. You cannot seeok on a field that is not in the index. It looks like you have an index on the order number and item, which I'd think is an int and a string, but you're passing in "delnote" and item, which are both strings.

撩人痒 2024-09-18 13:35:49

感谢您的回答,这两个字段都是字符串。我正在导入一个将订单号存储为字符串的后端。 delnote 字段的命名很糟糕(我最初正在处理送货单编号,但尚未重构变量名称)。我现在已经让这段代码正常工作了,我将其重写如下,我不确定为什么将字符串存储到对象中应该会产生任何影响,但它现在正在工作。

OT警告:SDF2.3与vs2010兼容吗?我们即将升级。

    public bool SeekOrderLine(object orderNum,object item)
    {
        bool isFound = false;

        cmd = new SqlCeCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.TableDirect;
        cmd.CommandText = "orderLines";
        cmd.IndexName = "orderline_idx";
        try
        {
            orderRS = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
            isFound = orderRS.Seek(DbSeekOptions.FirstEqual, new object[] {orderNum, item});
            if (isFound)
            {
                orderRS.Read();
                currentRowData = this.RetrieveRecord();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return isFound;
    }

Thanks for your answer, both fields are strings. I am importing into a backend that stores the order number as a string. The delnote field is just poorly named (I was working on delivery note numbers originally and hadn't refactored the variable name yet). I have now got this code working I re-wrote it as below, I'm not sure why storing the strings into object should make any difference but it is now working.

OT Warning: is SDF2.3 compatable with vs2010? we are upgrading soon.

    public bool SeekOrderLine(object orderNum,object item)
    {
        bool isFound = false;

        cmd = new SqlCeCommand();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.TableDirect;
        cmd.CommandText = "orderLines";
        cmd.IndexName = "orderline_idx";
        try
        {
            orderRS = cmd.ExecuteResultSet(ResultSetOptions.Scrollable | ResultSetOptions.Updatable);
            isFound = orderRS.Seek(DbSeekOptions.FirstEqual, new object[] {orderNum, item});
            if (isFound)
            {
                orderRS.Read();
                currentRowData = this.RetrieveRecord();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return isFound;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文