Home All Groups Group Topic Archive Search About

OdbcConnection - Querying MySql tables using date parameters

Author
4 Nov 2005 7:11 PM
Ben Fidge
Hi

I'm trying to query a MySql database using OdbcConnection. My query looks
like so:

select Count(*) from Table1 where BookingDate = @Date

I'm using an OdbcParameter set up as such:

OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
oParam.Value = dDate.Date;

Where dDate = new DateTime(2005, 11, 4);

I know there to be records matching this query in the database yet nothing
is returned. I'm new to MySql, coming from a SqlServer background. Am I
missing something obvious?

Thanks

Ben

Author
4 Nov 2005 7:29 PM
Jim Underwood
I am nto sure, but if your value is stored as a date/tiem field in the
database you may not get a match because the parameter and the database
columns have different tiem stamps but the same date value.

I always have trouble with this myself, as different databases seem to
handle it differently, but try forcing the date to a specific format on both
ends.  Mainly, truncate the date on the mysql side to insure that you are
not returning any time value then do the same with the C# code (I think you
are already), or convert the date to a string value on both ends.


Show quote
"Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
news:O4iHSOX4FHA.1276@TK2MSFTNGP09.phx.gbl...
> Hi
>
> I'm trying to query a MySql database using OdbcConnection. My query looks
> like so:
>
> select Count(*) from Table1 where BookingDate = @Date
>
> I'm using an OdbcParameter set up as such:
>
> OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
> oParam.Value = dDate.Date;
>
> Where dDate = new DateTime(2005, 11, 4);
>
> I know there to be records matching this query in the database yet nothing
> is returned. I'm new to MySql, coming from a SqlServer background. Am I
> missing something obvious?
>
> Thanks
>
> Ben
>
>
Author
4 Nov 2005 7:41 PM
Ben Fidge
Thanks for your response Jim.

While I was waiting, I tried issuing another query, and it appears that it
doesn't like using paramters. For example, the following worked:

    select count(*) from Table1 where ShowNo = 1

However, this didn't

    select count(*) from Table1 where ShowNo = 1

    ...

    OdbcParamter oParam = oCmd.Parameters.Add("@ShowNo", OdbcType.Int);
    oParam.Value = 1;

Any ideas where I'm going wrong here?

Ben


Show quote
"Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
news:O%23jOKYX4FHA.2352@TK2MSFTNGP12.phx.gbl...
>I am nto sure, but if your value is stored as a date/tiem field in the
> database you may not get a match because the parameter and the database
> columns have different tiem stamps but the same date value.
>
> I always have trouble with this myself, as different databases seem to
> handle it differently, but try forcing the date to a specific format on
> both
> ends.  Mainly, truncate the date on the mysql side to insure that you are
> not returning any time value then do the same with the C# code (I think
> you
> are already), or convert the date to a string value on both ends.
>
>
> "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
> news:O4iHSOX4FHA.1276@TK2MSFTNGP09.phx.gbl...
>> Hi
>>
>> I'm trying to query a MySql database using OdbcConnection. My query looks
>> like so:
>>
>> select Count(*) from Table1 where BookingDate = @Date
>>
>> I'm using an OdbcParameter set up as such:
>>
>> OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
>> oParam.Value = dDate.Date;
>>
>> Where dDate = new DateTime(2005, 11, 4);
>>
>> I know there to be records matching this query in the database yet
>> nothing
>> is returned. I'm new to MySql, coming from a SqlServer background. Am I
>> missing something obvious?
>>
>> Thanks
>>
>> Ben
>>
>>
>
>
Author
4 Nov 2005 7:45 PM
Jim Underwood
Again... I am not sure... but I think you need to use a ? where your
parameters are, and then set them in the order they appear...


Show quote
"Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
news:uxMjLfX4FHA.1184@TK2MSFTNGP12.phx.gbl...
> Thanks for your response Jim.
>
> While I was waiting, I tried issuing another query, and it appears that it
> doesn't like using paramters. For example, the following worked:
>
>     select count(*) from Table1 where ShowNo = 1
>
> However, this didn't
>
>     select count(*) from Table1 where ShowNo = 1
>
>     ...
>
>     OdbcParamter oParam = oCmd.Parameters.Add("@ShowNo", OdbcType.Int);
>     oParam.Value = 1;
>
> Any ideas where I'm going wrong here?
>
> Ben
>
>
> "Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
> news:O%23jOKYX4FHA.2352@TK2MSFTNGP12.phx.gbl...
> >I am nto sure, but if your value is stored as a date/tiem field in the
> > database you may not get a match because the parameter and the database
> > columns have different tiem stamps but the same date value.
> >
> > I always have trouble with this myself, as different databases seem to
> > handle it differently, but try forcing the date to a specific format on
> > both
> > ends.  Mainly, truncate the date on the mysql side to insure that you
are
> > not returning any time value then do the same with the C# code (I think
> > you
> > are already), or convert the date to a string value on both ends.
> >
> >
> > "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
> > news:O4iHSOX4FHA.1276@TK2MSFTNGP09.phx.gbl...
> >> Hi
> >>
> >> I'm trying to query a MySql database using OdbcConnection. My query
looks
> >> like so:
> >>
> >> select Count(*) from Table1 where BookingDate = @Date
> >>
> >> I'm using an OdbcParameter set up as such:
> >>
> >> OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
> >> oParam.Value = dDate.Date;
> >>
> >> Where dDate = new DateTime(2005, 11, 4);
> >>
> >> I know there to be records matching this query in the database yet
> >> nothing
> >> is returned. I'm new to MySql, coming from a SqlServer background. Am I
> >> missing something obvious?
> >>
> >> Thanks
> >>
> >> Ben
> >>
> >>
> >
> >
>
>
Author
4 Nov 2005 7:53 PM
Ben Fidge
Excellent, it works. I'm quite concerned about using ? to denote parameters,
however. It takes a lot of discipline to make sure everything is in the
right order. Especially if you're likely to change the query in the future.

I'm really missing Sql Server!!

Ben

Show quote
"Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
news:ubZLihX4FHA.252@TK2MSFTNGP15.phx.gbl...
> Again... I am not sure... but I think you need to use a ? where your
> parameters are, and then set them in the order they appear...
>
>
> "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
> news:uxMjLfX4FHA.1184@TK2MSFTNGP12.phx.gbl...
>> Thanks for your response Jim.
>>
>> While I was waiting, I tried issuing another query, and it appears that
>> it
>> doesn't like using paramters. For example, the following worked:
>>
>>     select count(*) from Table1 where ShowNo = 1
>>
>> However, this didn't
>>
>>     select count(*) from Table1 where ShowNo = 1
>>
>>     ...
>>
>>     OdbcParamter oParam = oCmd.Parameters.Add("@ShowNo", OdbcType.Int);
>>     oParam.Value = 1;
>>
>> Any ideas where I'm going wrong here?
>>
>> Ben
>>
>>
>> "Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
>> news:O%23jOKYX4FHA.2352@TK2MSFTNGP12.phx.gbl...
>> >I am nto sure, but if your value is stored as a date/tiem field in the
>> > database you may not get a match because the parameter and the database
>> > columns have different tiem stamps but the same date value.
>> >
>> > I always have trouble with this myself, as different databases seem to
>> > handle it differently, but try forcing the date to a specific format on
>> > both
>> > ends.  Mainly, truncate the date on the mysql side to insure that you
> are
>> > not returning any time value then do the same with the C# code (I think
>> > you
>> > are already), or convert the date to a string value on both ends.
>> >
>> >
>> > "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
>> > news:O4iHSOX4FHA.1276@TK2MSFTNGP09.phx.gbl...
>> >> Hi
>> >>
>> >> I'm trying to query a MySql database using OdbcConnection. My query
> looks
>> >> like so:
>> >>
>> >> select Count(*) from Table1 where BookingDate = @Date
>> >>
>> >> I'm using an OdbcParameter set up as such:
>> >>
>> >> OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
>> >> oParam.Value = dDate.Date;
>> >>
>> >> Where dDate = new DateTime(2005, 11, 4);
>> >>
>> >> I know there to be records matching this query in the database yet
>> >> nothing
>> >> is returned. I'm new to MySql, coming from a SqlServer background. Am
>> >> I
>> >> missing something obvious?
>> >>
>> >> Thanks
>> >>
>> >> Ben
>> >>
>> >>
>> >
>> >
>>
>>
>
>
Author
4 Nov 2005 8:08 PM
Jim Underwood
Glad to hear you have it working.

Please post a sample of your final code, so others can see what it looks
like when it is completed.


Show quote
"Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
news:uAKoBmX4FHA.1420@TK2MSFTNGP09.phx.gbl...
> Excellent, it works. I'm quite concerned about using ? to denote
parameters,
> however. It takes a lot of discipline to make sure everything is in the
> right order. Especially if you're likely to change the query in the
future.
>
> I'm really missing Sql Server!!
>
> Ben
>
> "Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
> news:ubZLihX4FHA.252@TK2MSFTNGP15.phx.gbl...
> > Again... I am not sure... but I think you need to use a ? where your
> > parameters are, and then set them in the order they appear...
> >
> >
> > "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
> > news:uxMjLfX4FHA.1184@TK2MSFTNGP12.phx.gbl...
> >> Thanks for your response Jim.
> >>
> >> While I was waiting, I tried issuing another query, and it appears that
> >> it
> >> doesn't like using paramters. For example, the following worked:
> >>
> >>     select count(*) from Table1 where ShowNo = 1
> >>
> >> However, this didn't
> >>
> >>     select count(*) from Table1 where ShowNo = 1
> >>
> >>     ...
> >>
> >>     OdbcParamter oParam = oCmd.Parameters.Add("@ShowNo", OdbcType.Int);
> >>     oParam.Value = 1;
> >>
> >> Any ideas where I'm going wrong here?
> >>
> >> Ben
> >>
> >>
> >> "Jim Underwood" <james.underw***@fallonclinic.com> wrote in message
> >> news:O%23jOKYX4FHA.2352@TK2MSFTNGP12.phx.gbl...
> >> >I am nto sure, but if your value is stored as a date/tiem field in the
> >> > database you may not get a match because the parameter and the
database
> >> > columns have different tiem stamps but the same date value.
> >> >
> >> > I always have trouble with this myself, as different databases seem
to
> >> > handle it differently, but try forcing the date to a specific format
on
> >> > both
> >> > ends.  Mainly, truncate the date on the mysql side to insure that you
> > are
> >> > not returning any time value then do the same with the C# code (I
think
> >> > you
> >> > are already), or convert the date to a string value on both ends.
> >> >
> >> >
> >> > "Ben Fidge" <ben.fidge@nospambtopenworld.com> wrote in message
> >> > news:O4iHSOX4FHA.1276@TK2MSFTNGP09.phx.gbl...
> >> >> Hi
> >> >>
> >> >> I'm trying to query a MySql database using OdbcConnection. My query
> > looks
> >> >> like so:
> >> >>
> >> >> select Count(*) from Table1 where BookingDate = @Date
> >> >>
> >> >> I'm using an OdbcParameter set up as such:
> >> >>
> >> >> OdbcParamter oParam = oCmd.Parameters.Add("@Date", OdbcType.Date);
> >> >> oParam.Value = dDate.Date;
> >> >>
> >> >> Where dDate = new DateTime(2005, 11, 4);
> >> >>
> >> >> I know there to be records matching this query in the database yet
> >> >> nothing
> >> >> is returned. I'm new to MySql, coming from a SqlServer background.
Am
> >> >> I
> >> >> missing something obvious?
> >> >>
> >> >> Thanks
> >> >>
> >> >> Ben
> >> >>
> >> >>
> >> >
> >> >
> >>
> >>
> >
> >
>
>

AddThis Social Bookmark Button