tradnbillies
Member
Is there a difference between:
Is there anything wrong with this code:
So I basically have two questions. I know I should be aware of the difference between "Dim a As A = New A()" and "Dim a As New A()" if there is one, but I am sort of self taught, and am questioning whether there is a difference there. Secondly, based on the code provided here, is there anyway I would have a BUNCH of open SQL Connections that are not closing on the SQL Server? This is the problem I am running into with my code, and I cannot figure out why. I can provide more details if possible, but please help me if you can.
Thanks,
Dave
VB.NET:
Dim myCommand As SqlCommand = New SqlCommand("myStoredProc", myConnection)
Dim myCommand As New SqlCommand("myStoredProc", myConnection)
Is there anything wrong with this code:
VB.NET:
Dim dbc As New dbConnect
Function doSomething(var As String) As String
dbc.ConnectIt()
Dim myCommand As New SqlCommand("myStoredProc", dbc.myConnection)
Try
myCommand.CommandType = CommandType.StoredProcedure
[SIZE=2]myCommand.Parameters.Add("@user", SqlDbType.VarChar).Value = var
[/SIZE] doSomething = myCommand.ExecuteScalar()
Catch ex As Exception
'myErrorHandlingCode
Finally
dbc.DisconnectIt()
myCommand.Dispose()
End Try
End Function
VB.NET:
[SIZE=2][COLOR=black]'[/COLOR][/SIZE][SIZE=2][COLOR=black]dbConnect class[/COLOR]
[/SIZE][COLOR=black][SIZE=2]Public [/SIZE][SIZE=2]Shared[/SIZE][SIZE=2] myConnection [/SIZE][SIZE=2]As [/SIZE][SIZE=2]New[/SIZE][/COLOR][SIZE=2][COLOR=black] SqlConnection(ConfigurationSettings.AppSettings("connectionString"))[/COLOR]
[/SIZE][COLOR=black][SIZE=2]Public [/SIZE][SIZE=2]Shared [/SIZE][SIZE=2]Sub[/SIZE][/COLOR][SIZE=2][COLOR=black] ConnectIt()[/COLOR]
[/SIZE][COLOR=black][SIZE=2] myConnection = [/SIZE][SIZE=2]New[/SIZE][/COLOR][SIZE=2][COLOR=black] SqlConnection(ConfigurationSettings.AppSettings("connectionString"))[/COLOR]
[/SIZE][COLOR=black] [SIZE=2]If[/SIZE][SIZE=2] myConnection.State = ConnectionState.Closed [/SIZE][/COLOR][SIZE=2][COLOR=black]Then[/COLOR]
[/SIZE][SIZE=2][COLOR=black] myConnection.Open()[/COLOR]
[/SIZE][SIZE=2][COLOR=black] End [/COLOR][/SIZE][SIZE=2][COLOR=black]If[/COLOR]
[/SIZE][COLOR=black][SIZE=2]End [/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[SIZE=2][COLOR=#0000ff][COLOR=black][SIZE=2]Public [/SIZE][SIZE=2]Shared [/SIZE][SIZE=2]Sub[/SIZE][/COLOR][SIZE=2][COLOR=black] DisconnectIt()[/COLOR]
[/SIZE][COLOR=black][SIZE=2] If[/SIZE][SIZE=2] myConnection.State = ConnectionState.Open [/SIZE][/COLOR][SIZE=2][COLOR=black]Then[/COLOR]
[/SIZE][SIZE=2][COLOR=black] myConnection.Close()[/COLOR]
[/SIZE][COLOR=black][SIZE=2] End [/SIZE][SIZE=2]If[/SIZE][/COLOR]
[SIZE=2][COLOR=#0000ff][COLOR=black]End Sub[/COLOR]
[/COLOR][/SIZE][/COLOR][/SIZE]
So I basically have two questions. I know I should be aware of the difference between "Dim a As A = New A()" and "Dim a As New A()" if there is one, but I am sort of self taught, and am questioning whether there is a difference there. Secondly, based on the code provided here, is there anyway I would have a BUNCH of open SQL Connections that are not closing on the SQL Server? This is the problem I am running into with my code, and I cannot figure out why. I can provide more details if possible, but please help me if you can.
Thanks,
Dave