Reputation: 63
I have an application where I store a value with date-type "as string"
The problem is that the results of the query aren't being shown correctly in C# when I execute the program, but when I apply the same query in mysql workbench the results are correct.
The date value is stored as a string in mysql; can we use comparison this way as shown or is it wrong?
string s = "select date from guest,program where guestid=guest.id AND *date >= " + date + "* "' ";
Upvotes: 4
Views: 278
Reputation: 38598
Since your column in the database is a date
column and the value you want to pass is a DateTime
strucure, as a good pratice, try to use parameters in your command. Using Parameters in your command, you avoid a lot of problems, like sql injection, type definition, etc. For sample:
string sql = "select date from guest,program where guestid=guest.id AND date >= @date";
using (MySqlConnection con = new MySqlConnection("your connectionstring"))
{
MySqlCommand cmd = new MySqlCommand(sql, con);
try
{
con.Open();
cmd.Parameters.AddWithValue("date", date);
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var dateField = (DateTime)reader["date"];
// some task
}
}
}
finally
{
con.Close();
}
}
Read more here: http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson06
Upvotes: 2