Wednesday, 7 August 2013

TSQL in the Command object

TSQL in the Command object

Please see the sub routine below:
Private Sub UpdateGrade(ByVal studentID As Integer, ByVal grade As String)
Dim objCommand As SqlCommand
Dim objCon As SqlConnection
Dim id As Integer
Dim _ConString As String
Try
_ConString =
ConfigurationManager.ConnectionStrings("TestConnection").ToString
objCon = New SqlConnection(_ConString)
objCommand = New SqlCommand("DECLARE @StudentID INT " & _
"DECLARE @Grade char(1) " & _
"SET @Grade = '" & grade & "'" & _
"SET @StudentID = '" & studentID & "'" & _
"If @Grade=1 " & _
"begin " & _
"update Student SET Grade = 'A' WHERE StudentID = @StudentID "
& _
"end " & _
"Else If @Grade=2 " & _
"begin " & _
"update Student SET Grade = 'B' WHERE StudentID = @StudentID "
& _
"end " & _
"If @Grade=3 " & _
"begin " & _
"update Student SET Grade = 'C' WHERE StudentID = @StudentID "
& _
"end " & _
"Else If @Grade=4 " & _
"begin " & _
"update Student SET Grade = 'D' WHERE StudentID = @StudentID "
& _
"end")
objCommand.Connection = objCon
objCon.Open()
objCommand.ExecuteNonQuery()
Catch ex As Exception
Throw
Finally
End Try
Is there anything setting the Command.CommandText to a TSQL statement? The
TSQL looks like this (for readibility):
DECLARE @StudentID INT
DECLARE @Grade char(1)
SET @Grade =
SET @StudentID =
If @Grade=1
begin
update Student SET Grade = 'A' WHERE StudentID = @StudentID
end
Else If @Grade=2
begin
update Student SET Grade = 'B' WHERE StudentID = @StudentID
end
If @Grade=3
begin
update Student SET Grade = 'C' WHERE StudentID = @StudentID
end
Else If @Grade=4
begin
update Student SET Grade = 'D' WHERE StudentID = @StudentID
end
There is a call to UpdateGrade in a while loop. The while loop, loops
through five million students. Please note that the live system is more
complex for this,so I have provided the code above for illustrative
purposes.

No comments:

Post a Comment