I am currently trying to grab some rows from a SQL Server database using C# that are of the following criteria:
- From the
RamResultsdatabase - in the
Resultstable - where the
Datecolumn is equal to the current date
I 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
@Form1.datemeans. For c#, the query is just a meaningless string. C# does not automatically replace this parameter with actual data. Some answers below show how to pass the parameter.