6

I am currently trying to grab some rows from a SQL Server database using C# that are of the following criteria:

  • From the RamResults database
  • in the Results table
  • where the Date column 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

3
  • 1
    Did you do any basic research on SQL SELECT syntax? msdn.microsoft.com/en-us/library/ms173294%28SQL.90%29.aspx Note it's the same as every other SQL mainstream SQL dialect... Commented Jan 2, 2012 at 17:01
  • == != =...in SQL == is =, also use parameters, what you have there won't work , sql server doesn't know what Form1.date is Commented Jan 2, 2012 at 17:01
  • SqlCE does not know anything about your form, so it will not understand what @Form1.date means. 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. Commented Jan 2, 2012 at 17:35

9 Answers 9

6

Description

2 things

  1. 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

  2. You forget to pass in the parameter.

Sample

// 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());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

5

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));
    // ...
}
// ...

Comments

2

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.

Comments

1

Not ==, use = for equation in SQL:

WHERE Date = @Form1.date

Comments

1

The operator "==" is invalid syntax for SQL. Use a single equal sign "=" in the where clause.

Comments

1

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

Comments

1

Replace your SQL Query with the following:

SELECT Result 
FROM RamResults 
WHERE Date like DATEADD(day, DATEDIFF(day, 0,  getdate()), 0)

Hope this works.

Comments

0

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

Comments

0

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());
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.