Skip to content

SQL Fill With Parameters Using SQL Query or a Stored Procedure

January 2, 2012
    '###########################################
    '      SQL Fill VB Code
    '###########################################
    'With SQL Query
    Public Shared Function GetData(ByVal Parameter As String) As DataTable
        GetDataTable = New DataTable
        Try
            Using da As New SqlDataAdapter("SELECT * FROM tblData WHERE Parameter = @Parameter ", Conn)
                da.SelectCommand.Parameters.AddWithValue("@Parameter", Parameter)
                da.Fill(GetDataTable)
            End Using
        Catch ex As Exception
            NotifyAdmin(String.Format("Computer: {0}</br>Application: {1}</br>Function/Sub: {2}</br>Error: {3}", Environment.MachineName, Application.ProductName, GetCurrentMethod.Name, ex))
        End Try
    End Function

    'With Stored Procedure
    Public Shared Function GetData(ByVal Parameter As String) As DataTable
        GetDataTable = New DataTable
        Try
            Using da As New SqlDataAdapter("spData", Conn)
                da.SelectCommand.CommandType = CommandType.StoredProcedure
                da.SelectCommand.Parameters.AddWithValue("@Parameter", Parameter)
                da.Fill(GetDataTable)
            End Using
        Catch ex As Exception
            NotifyAdmin(String.Format("Computer: {0}</br>Application: {1}</br>Function/Sub: {2}</br>Error: {3}", Environment.MachineName, Application.ProductName, GetCurrentMethod.Name, ex))
        End Try
    End Function
    
    --###########################################
    --      SQL Fill SQL Code
    --###########################################
CREATE PROCEDURE spData @Parameter VARCHAR(50)
AS
    BEGIN
        SELECT  *
        FROM    tblData
        WHERE   Parameter = @Parameter
    END
GO
    
Advertisements

From → .Net, SQL

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: