0

I'm using a MS Access database. I want to get first and last records that are in between 2 dates. But I get error when using a BETWEEN operator:

Syntax error in number in query expression 'Datum_k BETWEEN 3.4.2017. AND 3.4.2017.'.

My code:

private void GetPrviZadnjiBrojRacuna()
{
    OleDbCommand commandOD = new OleDbCommand("SELECT Dokument FROM DnevniPromet WHERE (Datum_k BETWEEN " + datumOd + " AND " + datumDo + ") ORDER BY [Datum_k] ASC", dataModel.CS);
    OleDbCommand commandDO = new OleDbCommand("SELECT Dokument FROM DnevniPromet WHERE [Datum_k] >= " + datumOd + " AND [Datum_k] <= " + datumDo + " ORDER BY [Datum_k] DESC", dataModel.CS);

    try
    {
        dataModel.DT.Clear();
        OleDbDataAdapter ODbDA = new OleDbDataAdapter(commandOD);

        if (!dataModel.CS.State.Equals(ConnectionState.Open))
        {
            dataModel.CS.Open();
        }

        // GET OD 
        ODbDA.Fill(dataModel.DT);
        odRacuna = dataModel.DT.Rows[0].ToString();

        // GET DO
        ODbDA.SelectCommand = commandDO; 
        dataModel.DT.Clear();
        ODbDA.Fill(dataModel.DT);

        doRacuna = dataModel.DT.Rows[0].ToString();

        dataModel.CS.Close();
        dataModel.DataLoaded = true;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}
4
  • 5
    Firstly, you should parameterise your queries, not use string interpolation. But you've missed the ' single quotes around the date variable values. Commented Apr 3, 2017 at 11:50
  • 6
    nononononono! never; never; never - never concatenate input into SQL. Never. But: yeah, that isn't valid SQL. I would say "that needs quotes", but I have a horrible suspicion you'd add them; what it actually needs is: parameters. We can't tell you the exact syntax you need unless you tell us what the provider is here; since you seem to be using OleDb, we can't infer anything. Can you tell us what the actual server is here? is it SQL Server? Oracle? ... or? Commented Apr 3, 2017 at 11:50
  • um isn't OleDb self-explanitory lol it's actually Access DB Commented Apr 3, 2017 at 11:54
  • 5
    no, "OleDb" is not self-explanatory - you can talk to just about anything via OleDb - it is a very generic transport Commented Apr 3, 2017 at 11:55

1 Answer 1

5

Addressing two issues with your code snippet:

  • You should never use string interpolation to build a query. This lends itself to SQL injection. Many, many topics surrounding this. I suggest you read up.
  • You have missed the ' single quotes around your date strings.

Using parameterised queries, you can kill two birds with one stone:

OleDbCommand commandOD = new OleDbCommand(@"
    SELECT Dokument
    FROM DnevniPromet
    WHERE (Datum_k BETWEEN @datumOd AND @datumDo)
    ORDER BY [Datum_k] ASC", dataModel.CS);

commandOD.Parameters.AddRange(new OleDbParameter[]
{
    new OleDbParameter("@datumOd", datumOd),
    new OleDbParameter("@datumDo", datumDo)
});
Sign up to request clarification or add additional context in comments.

2 Comments

welp that solved my issue even thought I never saw what was wrong in my original query to begin with - yes I'm lazy but I guess I need to start writing parameters.
With Access you should use # instead of ' when you are using date constant. msdn.microsoft.com/en-us/library/bb221200(v=office.12).aspx Of course using parameters is the best (the only right ?) way to go !

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.