【问题标题】:Get id of last inserted item in objectdatasource using DataSet使用 DataSet 获取 objectdatasource 中最后插入项的 id
【发布时间】:2011-06-14 05:08:33
【问题描述】:

我在 SO 上对她进行了很多搜索,但我似乎找不到任何这样做过的人。我确定这是一个常见问题,但我找不到正确的关键字来搜索解决方案,所以如果这是重复的,我很抱歉。

我有一个数据集,我在其中使用了“生成插入”、“更新”和“删除”语句以及“刷新数据表”选项,以便在使用 ObjectDatasource 时检索最后插入的 ID。

但是,当我处理 ObjectDataSource-Inserted 方法时,我得到一个 1 作为返回值,我认为这是受影响的行。我不想要那个,我想要这个 ID,以便我可以在我的代码中使用它。

我查看了 insert 方法的生成代码,插入后它正在执行 SELECT,但它返回整行而不是 ID,那么我怎样才能在这一行中检索值?

【问题讨论】:

  • 实际上有一个 ReturnValue 正在返回 -1... 项目已插入,但无论如何我总是得到 ReturnValue 的 1!
  • 你是在插入过程中调用存储过程,还是只是平面普通插入?
  • 我在定义初始选择命令时使用由数据集创建的标准 sql 生成方法。

标签: asp.net insert dataset objectdatasource


【解决方案1】:

我过去做过的事情是使用静态方法进行 CRUD 操作。

<ObjectDataSource runat="server" ID="_odsThingy"
        TypeName="Northwind.ThingyFactory" SelectMethod="Select" InsertMethod="Insert">
    <SelectParameters>
        <SessionParameter Name="id" SessionField="thingyId" />
    </SelectParameters>
</ObjectDataSource>

和 C# 代码:

class ThingyFactory {
    public static DataSet Select(int id, /* other criteria for selecting? */) {
        // ...
    }

    public static int Insert(/* values required for insert */) {
        // do insert & return the ID
    }
}

我没有尝试编译这段代码,所以如果我犯了一些错误,我深表歉意。由于获得了控制,这种特殊的模式使我摆脱了一些困境。请阅读this article 了解有关其工作原理的更多详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    相关资源
    最近更新 更多