I am currently trying to grab some rows from a SQL Server database using C# that are of the following criteria:
RamResults databaseResults tableDate column is equal to the current dateI have the following so far:
// Open the same connection with the same connection string.
using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
{
   con.Open();
   // Read specific values in the table.
   using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date == @Form1.date", con))
   {
      SqlCeDataReader reader = com.ExecuteReader();
      while (reader.Read())
      {
         int resultsoutput = reader.GetInt32(0);
         MessageBox.Show(resultsoutput.ToString());
      }
   }
}
Using SELECT Result FROM RamResults WHERE Date == Form1.date throws an error:
There was an error parsing the query. [ Token line number = 1,Token line offset = 43,Token in error = = ]
Although if I take out the WHERE statement e.g.
SELECT Result FROM RamResults
it works perfectly
2 things
Use = instead of == because this is the right equals operator in T-SQL.
Your Query should be like this
SELECT Result FROM RamResults WHERE Date = @Date
You forget to pass in the parameter.
// Open the same connection with the same connection string.
using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
{
    con.Open();
    // Read specific values in the table.
    using (SqlCeCommand com = new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @Date", con))
    {
        com.Parameters.AddWithValue("@Date", Form1.date);
        SqlCeDataReader reader = com.ExecuteReader();
        while (reader.Read())
        {
            int resultsoutput = reader.GetInt32(0);
            MessageBox.Show(resultsoutput.ToString());
        }
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With