插入后访问行 PK (SqlDataSource)

发布于 2024-09-26 15:06:40 字数 1568 浏览 3 评论 0原文

我需要能够获取 C# 中插入行的主键,到目前为止我还不确定如何获取。我已将 SELECT SCOPE_IDENTITY() 放入 SQL 查询中,但如何从 SqlDataSource_Inserting 方法访问它,以便将其存储在审核表中?在我的方法中,我现在只知道如何访问参数(e.Command.Parameters)

编辑:

我的参数存储在我的 ASP.NET 文件中,如下所示(一些摘录):

InsertCommand="INSERT INTO [NominalCode] ([VAXCode], [Reference], [CostCentre], [Department], [ReportingCategory]) VALUES (@VAXCode, @Reference, @CostCentre, @Department, @ReportingCategory)"

        <InsertParameters>
            <asp:ControlParameter ControlID="DetailsView1" Name="VAXCode" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="Reference" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="CostCentre" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="Department" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ReportingCategory" 
                PropertyName="SelectedValue" />
        </InsertParameters>

并且我在 C# 代码隐藏中填充参数

                command.Parameters.AddWithValue("@source", "Nominal");
            command.Parameters.AddWithValue("@action", "Insert");
            command.Parameters.AddWithValue("@item", fields);
            command.Parameters.AddWithValue("@userid", name);
            command.Parameters.AddWithValue("@timestamp", DateTime.Now);

谢谢

I need to be able to get the primary key of an nserted row in C#, and so far I'm unsure how. I've put SELECT SCOPE_IDENTITY() into my SQL query, but how do I access this from the SqlDataSource_Inserting method, so I can store it in an auditing table? In my method I only now how to access the parameters (e.Command.Parameters)

EDIT:

My parameters are stored in my ASP.NET file like so (some extracts):

InsertCommand="INSERT INTO [NominalCode] ([VAXCode], [Reference], [CostCentre], [Department], [ReportingCategory]) VALUES (@VAXCode, @Reference, @CostCentre, @Department, @ReportingCategory)"

        <InsertParameters>
            <asp:ControlParameter ControlID="DetailsView1" Name="VAXCode" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="Reference" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="CostCentre" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="Department" 
                PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="DetailsView1" Name="ReportingCategory" 
                PropertyName="SelectedValue" />
        </InsertParameters>

And I fill the parameters in C# codebehind

                command.Parameters.AddWithValue("@source", "Nominal");
            command.Parameters.AddWithValue("@action", "Insert");
            command.Parameters.AddWithValue("@item", fields);
            command.Parameters.AddWithValue("@userid", name);
            command.Parameters.AddWithValue("@timestamp", DateTime.Now);

Thanks

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

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

发布评论

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

评论(1

且行且努力 2024-10-03 15:06:40

您可以通过命令参数访问它,方法是添加一个带有 输出方向(或者,如果您不返回其他结果(例如受影响的行),则可以使用 ReturnValue)。

IE。将这三行添加到参数列表的末尾

SqlParameters pKey = new SqlParameters("@pKey", System.Data.SqlDbType.Int);
pKey.Direction = Output;
command.Parameters.Add(pKey);

然后在 sql 的末尾,将该参数设置为主键值,如下所示:

set @pKey = scope_identity();

即。

 InsertCommand="INSERT INTO [NominalCode] ([VAXCode], [Reference], [CostCentre], [Department], [ReportingCategory]) VALUES (@VAXCode, @Reference, @CostCentre, @Department, @ReportingCategory); set @pKey = scope_identity()"

然后在执行命令后读回它:

int primaryKey = (int)pKey.Value;

简单!

进行审核的另一种方法是让存储过程直接执行审核捕获,以确保一致性并保存在所有应用程序中执行此操作,但这完全取决于偏好和系统设计。

You can access it via the Command Parameters by adding a new one with a Direction of Output, (or you can use ReturnValue if you're not returning another result, like rows affected).

ie. Add these three rows to the end of your parameters list

SqlParameters pKey = new SqlParameters("@pKey", System.Data.SqlDbType.Int);
pKey.Direction = Output;
command.Parameters.Add(pKey);

Then at the end of your sql, set that parameter to the Primary Key value like so:

set @pKey = scope_identity();

ie.

 InsertCommand="INSERT INTO [NominalCode] ([VAXCode], [Reference], [CostCentre], [Department], [ReportingCategory]) VALUES (@VAXCode, @Reference, @CostCentre, @Department, @ReportingCategory); set @pKey = scope_identity()"

Then just read it back after you've executed the command:

int primaryKey = (int)pKey.Value;

Simples!

Another way to do auditing is to have the stored proc perform the audit capture directly, to ensure consistency and to save doing it in all the apps but this is all down to preference and system design.

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