2
我使用範圍標識的概念如下。使用支持我的概念的範圍標識的正確方法是什麼?如何在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