Reputation: 2396
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
Upvotes: 6
Views: 11977
Reputation: 247
I found two things in your code which is creating a problem
1) assign values to parameter 2) == instead of = (just to make it working appropriately with SQL )
so the code should be like this :
using (SqlCeConnection con = new SqlCeConnection(DatabaseControl.conString))
{
con.Open();
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());
}
}
}
Upvotes: 0
Reputation: 11
That is quite confusing and many people commit these kind of mistakes. While C# uses == for equality Operations (Ok We do have Equal() as well), SQL uses just = for it.
Apart from this, you also forgot to pass the parameter here
Upvotes: 0
Reputation: 1127
Replace your SQL Query with the following:
SELECT Result
FROM RamResults
WHERE Date like DATEADD(day, DATEDIFF(day, 0, getdate()), 0)
Hope this works.
Upvotes: 1
Reputation: 60556
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());
}
}
}
Upvotes: 6
Reputation: 57843
Try this:
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())
Always use SQL parameters instead of string concatenation.
Upvotes: 2
Reputation: 2387
Try
var query = "SELECT Result FROM RamResults WHERE Date = " + Form1.date;
using (SqlCeCommand com = new SqlCeCommand(query, con))
I would suggest using parameters as in this example on MSDN
Upvotes: 1
Reputation: 10780
The operator "==" is invalid syntax for SQL. Use a single equal sign "=" in the where clause.
Upvotes: 1
Reputation: 51694
Try parameterizing your query and replace ==
with =
in your WHERE
clause:
// ...
using (SqlCeCommand com =
new SqlCeCommand("SELECT Result FROM RamResults WHERE Date = @date", con))
{
com.Parameters.Add(new SqlParameter("date", Form1.date));
// ...
}
// ...
Upvotes: 5