如何在 SQL 命令中使用作用域标识?
下面给出了我使用范围标识的概念。使用支持我的概念的范围标识的正确方法是什么?
对于单个数据:
Imports System.Data.SqlClient
Public Class Class1
Sub Something()
Using con As SqlConnection = New SqlConnection("ConnnectionString")
Dim tran As SqlTransaction = con.BeginTransaction("ATransaction")
Using cmd As SqlCommand = New SqlCommand("DECLARE @ScopeId bigint;INSERT INTO AuditEvents(UserId) VALUES(@UserId);SELECT @ScopeId=SCOPE_IDENTITY();")
cmd.Parameters.AddWithValue("@UserId", 1)
cmd.Transaction = tran
For rowNumber As Integer = 0 To 5 'DataGridView.Rows.Count - 1
Next
Using childCommand As SqlCommand = New SqlCommand("INSERT INTO AuditEventDetails(EventId, ResourceName, OldValue, NewValue) SELECT @EventId, @ResourceName, @OldValue, @NewValue")
childCommand.Parameters.AddWithValue("@EventId", "@ScopeId") '???????
childCommand.Parameters.AddWithValue("@ResourceName", "Something")
childCommand.Parameters.AddWithValue("@OldValue", "OldValue")
childCommand.Parameters.AddWithValue("@NewValue", "NewValue")
'............................................................................
'............................................................................
'............................................................................
'............................................................................
End Using
End Using
End Using
End Sub
End Class
对于多个数据:
Imports System.Data.SqlClient
Public Class Class1
Sub Something()
Using con As SqlConnection = New SqlConnection("ConnnectionString")
Dim tran As SqlTransaction = con.BeginTransaction("ATransaction")
Using cmd As SqlCommand = New SqlCommand("DECLARE @ScopeId bigint;INSERT INTO AuditEvents(UserId) VALUES(@UserId);SELECT @ScopeId=SCOPE_IDENTITY();")
cmd.Parameters.AddWithValue("@UserId", 1)
cmd.Transaction = tran
For rowNumber As Integer = 0 To 5 'DataGridView.Rows.Count - 1
Using childCommand As SqlCommand = New SqlCommand("INSERT INTO AuditEventDetails(EventId, ResourceName, OldValue, NewValue) SELECT @EventId, @ResourceName, @OldValue, @NewValue")
childCommand.Parameters.AddWithValue("@EventId", "@ScopeId") '???????
childCommand.Parameters.AddWithValue("@ResourceName", "Something")
childCommand.Parameters.AddWithValue("@OldValue", "OldValue")
childCommand.Parameters.AddWithValue("@NewValue", "NewValue")
'............................................................................
'............................................................................
'............................................................................
'............................................................................
Next
End Using
End Using
End Using
End Sub
End Class
My concept for using the scope identity is given below. What is the proper way to use the scope identity that support my concept?
For single data:
Imports System.Data.SqlClient
Public Class Class1
Sub Something()
Using con As SqlConnection = New SqlConnection("ConnnectionString")
Dim tran As SqlTransaction = con.BeginTransaction("ATransaction")
Using cmd As SqlCommand = New SqlCommand("DECLARE @ScopeId bigint;INSERT INTO AuditEvents(UserId) VALUES(@UserId);SELECT @ScopeId=SCOPE_IDENTITY();")
cmd.Parameters.AddWithValue("@UserId", 1)
cmd.Transaction = tran
For rowNumber As Integer = 0 To 5 'DataGridView.Rows.Count - 1
Next
Using childCommand As SqlCommand = New SqlCommand("INSERT INTO AuditEventDetails(EventId, ResourceName, OldValue, NewValue) SELECT @EventId, @ResourceName, @OldValue, @NewValue")
childCommand.Parameters.AddWithValue("@EventId", "@ScopeId") '???????
childCommand.Parameters.AddWithValue("@ResourceName", "Something")
childCommand.Parameters.AddWithValue("@OldValue", "OldValue")
childCommand.Parameters.AddWithValue("@NewValue", "NewValue")
'............................................................................
'............................................................................
'............................................................................
'............................................................................
End Using
End Using
End Using
End Sub
End Class
For multiple data:
Imports System.Data.SqlClient
Public Class Class1
Sub Something()
Using con As SqlConnection = New SqlConnection("ConnnectionString")
Dim tran As SqlTransaction = con.BeginTransaction("ATransaction")
Using cmd As SqlCommand = New SqlCommand("DECLARE @ScopeId bigint;INSERT INTO AuditEvents(UserId) VALUES(@UserId);SELECT @ScopeId=SCOPE_IDENTITY();")
cmd.Parameters.AddWithValue("@UserId", 1)
cmd.Transaction = tran
For rowNumber As Integer = 0 To 5 'DataGridView.Rows.Count - 1
Using childCommand As SqlCommand = New SqlCommand("INSERT INTO AuditEventDetails(EventId, ResourceName, OldValue, NewValue) SELECT @EventId, @ResourceName, @OldValue, @NewValue")
childCommand.Parameters.AddWithValue("@EventId", "@ScopeId") '???????
childCommand.Parameters.AddWithValue("@ResourceName", "Something")
childCommand.Parameters.AddWithValue("@OldValue", "OldValue")
childCommand.Parameters.AddWithValue("@NewValue", "NewValue")
'............................................................................
'............................................................................
'............................................................................
'............................................................................
Next
End Using
End Using
End Using
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用存储过程,那么我认为最好使用输出参数。
不过,对于基于文本的 SQL 命令,我倾向于使用类似的命令;
If you use stored procs, then I think it is preferable to use output parameters.
For text based SQL commands though, I tend to use something like;