I need reduce significantly this code, is there any way to create a sql parameter describing its direction? Here is the code:
Dim Oparam1 As SqlParameter = New SqlParameter("@ROJO", SqlDbType.Int)
Dim Oparam2 As SqlParameter = New SqlParameter("@AMBAR", SqlDbType.Int)
Dim Oparam3 As SqlParameter = New SqlParameter("@AMARILLO", SqlDbType.Int)
Dim Oparam4 As SqlParameter = New SqlParameter("@VERDE", SqlDbType.Int)
Oparam1.Direction = ParameterDirection.Output
Oparam2.Direction = ParameterDirection.Output
Oparam3.Direction = ParameterDirection.Output
Oparam4.Direction = ParameterDirection.Output
command.Parameters.Add(Oparam1)
command.Parameters.Add(Oparam2)
command.Parameters.Add(Oparam3)
command.Parameters.Add(Oparam4)
thanks in advance.
There is an overload of the constructor that includes the direction, but then you have to specify a lot of other parameters, so the code won't be any shorter after all.
You can make an extension method:
Imports System.Runtime.CompilerServices
Module SqlExtensions
<Extension()>
Public Function SetOutput(parameter As SqlParameter) As SqlParameter
parameter.Direction = ParameterDirection.Output
Return parameter
End Function
End Module
Now you can use that on the parameters:
command.Parameters.Add(New SqlParameter("@ROJO", SqlDbType.Int).SetOutput())
command.Parameters.Add(New SqlParameter("@AMBAR", SqlDbType.Int).SetOutput())
command.Parameters.Add(New SqlParameter("@AMARILLO", SqlDbType.Int).SetOutput())
command.Parameters.Add(New SqlParameter("@VERDE", SqlDbType.Int).SetOutput())