>Hi Folks.
>I am trying to use paramters in a SQL statement bu am getting an error:
>SqlException (0x80131904): Must declare the scalar variable
>
>My syntax is like:
> lcSelect = "DOC_DATE > = " + " @ldFrom " + " AND DOC_DATE < = " + " @ldTo " ;
>....This is added to the other bits of a a sql statement
>The ldFrom and ldTo are local datetime variables which have been initialised from a datetime session variable
>
>Am I missing something here ? Do I have to issue some sort of a parameter statement somewhere else ?
>
>Regards,
>Gerard
Gerard,
This is not what I meant when I said use parameters. You are not using parameters with this code, you're only hardcoding again usişng variables. What I meant was like this:
OleDbCommand cmd =
new OleDbCommand("select * from myTable where DOC_Date between ? and ?", con );
cmd.Parameters.Add("dFrom",OleDbType.Date).Value = DateTime.Today.AddDays(-10);
cmd.Parameters.Add("dTo",OleDbType.Date).Value = DateTime.Today;
With SqlProvider you can also use named parameters.
Cetin