C# dynamic SQL from Form Input

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

I have a date field on a C# form I would like to use in the WHERE
clause of a SQL statement going against Access2002. Do I need to
perform a todate() type funtion on the textbox field on the form? What
would the SQL statement look like?

Thanks in advance - Dan
 
Dan,

You should be using a parameterized query for your SQL statement.
Basically, you would create a SqlCommand (or OleDbCommand, etc, etc), with
the command text of the command like this:

select * from table where datefield = @datefield

Then, you would add the appropriate parameter to the parameters
collection on the command, and set the value of that to a DateTime instance.
When you execute the command, the provider will be responsible for
performing the necessary conversions in the sql statement to a
representation that the database server can understand.

Hope this helps.
 
First off, don't use Dynamic Sql, use Parameters ..
http://www.knowdotnet.com/articles/storedprocsvb.html

YOu can use the same methodology that's used here, just set the command
type to text and use ? whereever you want the param to appear

cmd.CommandText = "SELECT * FROM myTable WHERE SOmeDate = ?";

cmd.Parameters.Add(CType(tbMyDateTextBox.text, DateTime))

HTH,

Bill
 
Back
Top