Never pass dates as string, but use the proper datetime datatype. Look up commands and parameters instead of creating SQL statements in code. Google SqlCommand
EDIT: I was on my phone yesterday. Here is a better answer:
int GetNumberOfItemsAfterDate(DateTime dateInput) {
// Create a sql statement with a parameter (@FirstDate)
string sql = @"SELECT Count(*)
FROM table
WHERE table.date >= @FirstDate";
// Create a sql connection
// Always use the using statement for this
using (var connection = new SqlConnection(connectionString)) {
// Create a sql command
// Always use the using statement for this
using (var command = new SqlCommand(sql, connection)) {
// Pass the dateinput argument to the command, and let
// the .NET Framework handle the conversion properly!
command.Parameters.Add(new SqlCommand("FirstDate", dateInput));
// No need for calling .Close() or .Dispose() - the using
// statements handle that for us
return (int)command.ExecuteScalar();
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…