Winston Madiano
Winston Madiano

Reputation: 87

Set SqlParameter in VB.NET?

I'm new with classes and I want to create a SqlCommandManager class and I can't figure out how to pass SqlParameter on my class.

For example if I want to insert data I would just use my class like client below.

'Client

Dim m_SqlComManager as new SQLCommandManager("MyConnectionString")
m_SqlCommandManager.Commandtext = "INSERT INTO [TableName]([Field1],[Field2])VALUES(@Field1,Field2);"
m_SqlCommandManager.Parameters.AddWithValue("@Field1","SomeValue1")
m_SqlCommandManager.Parameters.AddWithValue("@Field2","SomeValue2")
m_SqlCommandManager.ExecuteNonQuery()


'Here is my class

Imports System.Data.SqlClient
Public Class SQLCommandManager
Private m_SqlParameters As SqlParameter()
Private m_Commandtext As String
Private m_ConStr As String

Public WriteOnly Property SQlParameter() As SqlParameter()
    Set(ByVal value As SqlParameter())
        value = m_SqlParameters
    End Set
End Property

Public Property CommandText() As String
    Get
        Return m_Commandtext
    End Get
    Set(ByVal value As String)
        value = m_Commandtext
    End Set
End Property

Public Sub New(ByVal con As String)
    m_ConStr = con
End Sub

Public Sub ExecuteNonQuery()
    Using con As New SqlConnection(m_ConStr)
        Using com As New SqlCommand
            com.Connection = con
            com.CommandText = m_Commandtext
            'Please help
            'How can i insert parameter here from client..

            If con.State = ConnectionState.Closed Then
                con.Open()
            End If
            com.ExecuteNonQuery()
        End Using
    End Using
End Sub
End Class

How how can I set the parameters before the ExecuteNonQuery method?

Thanks in advance..

Upvotes: 1

Views: 6284

Answers (1)

marc_s
marc_s

Reputation: 754598

I would do something like this:

Public Class SqlCommandManager
    Private m_SqlParameters As List(Of SqlParameter)
    Private m_Commandtext As String
    Private m_ConStr As String

    Public Sub New()
        m_SqlParameters = New List(Of SqlParameter)()
    End Sub

    Public ReadOnly Property SqlParameters() As List(Of SqlParameter)
        Get
            Return m_SqlParameters
        End Get
    End Property

    Public Property CommandText() As String
        Get
            Return m_Commandtext
        End Get
        Set
            value = m_Commandtext
        End Set
    End Property

    Public Sub New(con As String)
        m_ConStr = con
    End Sub

    Public Sub ExecuteNonQuery()
        Using con As New SqlConnection(m_ConStr)
            Using com As New SqlCommand(m_Commandtext, con)
                com.Parameters.AddRange(m_SqlParameters.ToArray())

                con.Open()
                com.ExecuteNonQuery()
                con.Close()
            End Using
        End Using
    End Sub
End Class

What I've changed:

  • Changed the class name to SqlCommandManager to be in line with Microsoft's recommendations (don't capitalize more than 2 letters in an abbreviation; IO is fine, Sql and Xml should not be all capitalized)

  • I would use a List(Of SqlParameter) rather than an array - much easier to deal with, much easier to add additional parameters to it

  • I prefer to pass the CommandText and the SqlConnection right into the constructor of the SqlCommand - that way,you definitely never forget these two vital bits of information!

  • Just before your .ExecuteQuery, add the parameters defined in your list to the parameter array of the SqlCommand using a single call to .AddRange()

Upvotes: 1

Related Questions