I think you're just missing it by a bit here Juan.
Two things are going on:
1. You are creating a SqlCommand object, that will become a SQL statement that is executed on SQL Server:
Using cmd As New SqlCommand
'-- This builds a sql string (see below)
Dim strSQLStringWithParameters = "SELECT *"
strSQLStringWithParameters &= " FROM Sales"
strSQLStringWithParameters &= " WHERE Payment = 1"
strSQLStringWithParameters &= " AND Month(DatePay) = @pCurrenthMonth -- parameter"
strSQLStringWithParameters &= " AND Year(DatePay)= @pCurrentYear -- parameter"
This will end up as the following SQL statement:
SELECT *
FROM Sales
WHERE Payment = 1
AND Month(DatePay) = @pCurrentMonth -- parameter
AND Year(DatePay) = @pCurrentYear -- parameter
2. You are defining the values used by the parameters. Here you define parameters that will end up in SQL, with values you are providing via .NET variables.
'-- This defines what the parameters are for the SQL string
' The parameter name, @pCurrentMonth & @pCurrentYear, must match parameter used in SQL statement above.
cmd.Parameters.AddWithValue("@pCurrenthMonth", currenthmonth).SqlDbType = SqlDbType.Int
cmd.Parameters.AddWithValue("@pCurrentYear", currentyear).SqlDbType = SqlDbType.Int
'-- fill table
End Using
Note that currentmonth and currentyear are just .net variables. You can pass them into the fill method or pass in a date and then get the month/year or as you originally showed, just use the current date. They are just .NET variables, handled however makes sense in your situation.
No matter how you load up the currentmonth/currentyear variables, their values are passed along to SQL Server as parameters. The Parameters.AddWithValue methods essentially gets translated into SQL again (assuming that currentmonth was set to 10 and currentyear was 2009:
Declare @pCurrentMonth int
Declare @pCurrentYear int
Set @pCurrentMonth = 10
Set @pCurrentYear = 2009
The important thing is that you define parameters within the SQL statement (" AND Month(DatePay) = @pCurrentMonth") and then also define a parameter of the correct SQL data type with the same name ("cmd.Parameters.AddWithValue("@pCurrentMonth", currentmonth)").