Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Query, DateTime

Query, DateTime

Scheduled Pinned Locked Moved C#
databasetutorialhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DougW48
    wrote on last edited by
    #1

    I'm posting this with the risk of Heath getting angry at me because for this one instance I'm not using the parameters in the dataAdapter...not because I like the old way of constructing a long select command, but because I don't know how to use the parameters in this circumstance. I am querying a database, no big deal. But, there are like 7 or 8 different "filter" parameters the user may choose from...and they can also choose to not use any of them. I didn't know how to tell the adapter to ignore that particular parameter, so I resorted to constructing my own select command. However (and Heath will probably say I told you so ;) ) two of these parameters are dates...I am trying to search BETWEEN Date1 AND Date2. This is giving me some really strange results though. First of all, I have to set Date1 all the way to the day before if I hope to get the correct results. Also, sometimes a perfectly good date range will return nothing, whereas a ridiculous range that hasn't even happened yet, will return rows with dates that don't fall anywhere in between my specified range. I'm sort of confused by it, so if anybody has faced this issue before, I'd like to know how it was solved....or, if I can somehow "turn-off" parameters at run time, or if I can see a small example, one or two lines is all, of how to create a parameter to the adapter at run time (peferrably a BETWEEN AND parameter), I'd be much obliged. And Heath, I know you get really frustrated when people don't use the adapter parameters, cause that's what they are there for, sorry about that.

    M H 2 Replies Last reply
    0
    • D DougW48

      I'm posting this with the risk of Heath getting angry at me because for this one instance I'm not using the parameters in the dataAdapter...not because I like the old way of constructing a long select command, but because I don't know how to use the parameters in this circumstance. I am querying a database, no big deal. But, there are like 7 or 8 different "filter" parameters the user may choose from...and they can also choose to not use any of them. I didn't know how to tell the adapter to ignore that particular parameter, so I resorted to constructing my own select command. However (and Heath will probably say I told you so ;) ) two of these parameters are dates...I am trying to search BETWEEN Date1 AND Date2. This is giving me some really strange results though. First of all, I have to set Date1 all the way to the day before if I hope to get the correct results. Also, sometimes a perfectly good date range will return nothing, whereas a ridiculous range that hasn't even happened yet, will return rows with dates that don't fall anywhere in between my specified range. I'm sort of confused by it, so if anybody has faced this issue before, I'd like to know how it was solved....or, if I can somehow "turn-off" parameters at run time, or if I can see a small example, one or two lines is all, of how to create a parameter to the adapter at run time (peferrably a BETWEEN AND parameter), I'd be much obliged. And Heath, I know you get really frustrated when people don't use the adapter parameters, cause that's what they are there for, sorry about that.

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      If you post your SELECT statement, it'll make it easier to help spot the problem. Michael CP Blog [^]

      D 1 Reply Last reply
      0
      • D DougW48

        I'm posting this with the risk of Heath getting angry at me because for this one instance I'm not using the parameters in the dataAdapter...not because I like the old way of constructing a long select command, but because I don't know how to use the parameters in this circumstance. I am querying a database, no big deal. But, there are like 7 or 8 different "filter" parameters the user may choose from...and they can also choose to not use any of them. I didn't know how to tell the adapter to ignore that particular parameter, so I resorted to constructing my own select command. However (and Heath will probably say I told you so ;) ) two of these parameters are dates...I am trying to search BETWEEN Date1 AND Date2. This is giving me some really strange results though. First of all, I have to set Date1 all the way to the day before if I hope to get the correct results. Also, sometimes a perfectly good date range will return nothing, whereas a ridiculous range that hasn't even happened yet, will return rows with dates that don't fall anywhere in between my specified range. I'm sort of confused by it, so if anybody has faced this issue before, I'd like to know how it was solved....or, if I can somehow "turn-off" parameters at run time, or if I can see a small example, one or two lines is all, of how to create a parameter to the adapter at run time (peferrably a BETWEEN AND parameter), I'd be much obliged. And Heath, I know you get really frustrated when people don't use the adapter parameters, cause that's what they are there for, sorry about that.

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        I won't badger you this time, but I still suggest you try it. Once you do, I suspect you'll never turn back. :) Dates in SQL Server are single-quoted like strings. When referring to Date fields, SQL Server knows what to do. For Access (and some other's I've seen, but don't remember) you encase them in pounds (or hashes - #). If you don't, they will be treated like strings (IIRC) so you won't get the result you'd expect.

        Microsoft MVP, Visual C# My Articles

        D 1 Reply Last reply
        0
        • M Michael P Butler

          If you post your SELECT statement, it'll make it easier to help spot the problem. Michael CP Blog [^]

          D Offline
          D Offline
          DougW48
          wrote on last edited by
          #4

          Putting pound signs around the Date like Heath suggested made my query a little better...I no longer have to subtract a day from Time1 to get the correct results. But here is the strange result I'm getting: A row with a date column of 5/4/2004 is being selected when the date value of Time2 is 5/11/2004 and Time1 is anything from 5/4/2004 to 5/10/2004, but it's not selecting my row when Time1 is before 5/4/2004, and it should. Here is the statement I'm using to select my rows. I hope you can find something, because I sure can't: string MySQLText = "SELECT * FROM Invoices"; DateTime Time1 = new DateTime(dateTimePicker1.Value.Year, dateTimePicker1.Value.Month, dateTimePicker1.Value.Day, 0, 0, 0); DateTime Time2 = new DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, dateTimePicker2.Value.Day, 23, 59, 59); MySQLText += " WHERE (OrderDate BETWEEN #" + Time1.ToString() + "# AND #" + Time2.ToString() + "#)"; oleDbSelectCommand1.CommandText = MySQLText; oleDbDataAdapter11.Fill( MyDataset );

          1 Reply Last reply
          0
          • H Heath Stewart

            I won't badger you this time, but I still suggest you try it. Once you do, I suspect you'll never turn back. :) Dates in SQL Server are single-quoted like strings. When referring to Date fields, SQL Server knows what to do. For Access (and some other's I've seen, but don't remember) you encase them in pounds (or hashes - #). If you don't, they will be treated like strings (IIRC) so you won't get the result you'd expect.

            Microsoft MVP, Visual C# My Articles

            D Offline
            D Offline
            DougW48
            wrote on last edited by
            #5

            Even with the pound signs, the adapter is treating the dates as strings. It is acting like 5/11/04 should go BEFORE 5/4/04. If using the parameters will fix this, I'll change to them as long as I can add them to the adapter programmatically, because in truth I would prefer to use the parameters...I just didn't know how in this situation. What would you suggest? Thank :)

            H 1 Reply Last reply
            0
            • D DougW48

              Even with the pound signs, the adapter is treating the dates as strings. It is acting like 5/11/04 should go BEFORE 5/4/04. If using the parameters will fix this, I'll change to them as long as I can add them to the adapter programmatically, because in truth I would prefer to use the parameters...I just didn't know how in this situation. What would you suggest? Thank :)

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Trust me - using parameters is so much better. You can add them programmatically, as there is no other way! :) For example (and I'm assuming you're using an OleDbCommand since you're using # signs):

              OleDbCommand cmd = oleDbConnection.CreateCommand();
              cmd.CommandText = "SELECT * FROM MyTable WHERE DateCol BETWEEN ? AND ?";
              cmd.Parameters.Add("StartDate", OleDbType.DBDate).Value = DateTime.Parse("5/4/04");
              cmd.Parameters.Add("EndDate", OleDbType.DBDate).Value = DateTime.Parse("5/11/04");
              OleDbDataReader reader = null;
              try
              {
              oleDbConnection.Open();
              reader = cmd.ExecuteReader();
              // ...
              }
              catch (Exception e)
              {
              Console.Error.WriteLine(e.Message);
              }
              finally
              {
              if (reader != null) reader.Close();
              oleDbConnection.Close();
              }

              Of course, you could use this command with an OleDbDataAdapter as well - this is just an example. Still, though it is unusual that it's treating it as a string. You're not using quotes and # signs, are you?

              Microsoft MVP, Visual C# My Articles

              D 2 Replies Last reply
              0
              • H Heath Stewart

                Trust me - using parameters is so much better. You can add them programmatically, as there is no other way! :) For example (and I'm assuming you're using an OleDbCommand since you're using # signs):

                OleDbCommand cmd = oleDbConnection.CreateCommand();
                cmd.CommandText = "SELECT * FROM MyTable WHERE DateCol BETWEEN ? AND ?";
                cmd.Parameters.Add("StartDate", OleDbType.DBDate).Value = DateTime.Parse("5/4/04");
                cmd.Parameters.Add("EndDate", OleDbType.DBDate).Value = DateTime.Parse("5/11/04");
                OleDbDataReader reader = null;
                try
                {
                oleDbConnection.Open();
                reader = cmd.ExecuteReader();
                // ...
                }
                catch (Exception e)
                {
                Console.Error.WriteLine(e.Message);
                }
                finally
                {
                if (reader != null) reader.Close();
                oleDbConnection.Close();
                }

                Of course, you could use this command with an OleDbDataAdapter as well - this is just an example. Still, though it is unusual that it's treating it as a string. You're not using quotes and # signs, are you?

                Microsoft MVP, Visual C# My Articles

                D Offline
                D Offline
                DougW48
                wrote on last edited by
                #7

                I'll give this a try and see if it makes a difference. And no, I just have the hash signs, not the single quotes. The message I replied after the other person's post is a direct copy and paste.

                1 Reply Last reply
                0
                • H Heath Stewart

                  Trust me - using parameters is so much better. You can add them programmatically, as there is no other way! :) For example (and I'm assuming you're using an OleDbCommand since you're using # signs):

                  OleDbCommand cmd = oleDbConnection.CreateCommand();
                  cmd.CommandText = "SELECT * FROM MyTable WHERE DateCol BETWEEN ? AND ?";
                  cmd.Parameters.Add("StartDate", OleDbType.DBDate).Value = DateTime.Parse("5/4/04");
                  cmd.Parameters.Add("EndDate", OleDbType.DBDate).Value = DateTime.Parse("5/11/04");
                  OleDbDataReader reader = null;
                  try
                  {
                  oleDbConnection.Open();
                  reader = cmd.ExecuteReader();
                  // ...
                  }
                  catch (Exception e)
                  {
                  Console.Error.WriteLine(e.Message);
                  }
                  finally
                  {
                  if (reader != null) reader.Close();
                  oleDbConnection.Close();
                  }

                  Of course, you could use this command with an OleDbDataAdapter as well - this is just an example. Still, though it is unusual that it's treating it as a string. You're not using quotes and # signs, are you?

                  Microsoft MVP, Visual C# My Articles

                  D Offline
                  D Offline
                  DougW48
                  wrote on last edited by
                  #8

                  I'm pretty embarrassed to admit this, but I discovered that the date column in my database was set to text. At any rate, I did convert things to parameters, so your efforts weren't in vain. Thanks for the help!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups