插入后访问行 PK (SqlDataSource)
我需要能够获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过命令参数访问它,方法是添加一个带有 输出方向(或者,如果您不返回其他结果(例如受影响的行),则可以使用 ReturnValue)。
IE。将这三行添加到参数列表的末尾
然后在 sql 的末尾,将该参数设置为主键值,如下所示:
即。
然后在执行命令后读回它:
简单!
进行审核的另一种方法是让存储过程直接执行审核捕获,以确保一致性并保存在所有应用程序中执行此操作,但这完全取决于偏好和系统设计。
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
Then at the end of your sql, set that parameter to the Primary Key value like so:
ie.
Then just read it back after you've executed the command:
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.