Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2005, 02:40 AM
spc spc is offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Unable to read Time field value from MySQL DB

 Hi All,

  I have developed a Migration Tool that migrates data from MySQL to MSSQL. But I am finding a problem to read the time field value from MySQL database . I was using DataSet(.Net framework) for my application to retrieve the records from database & storing them into that. Although the values are there in MySQL DB , the value I got while reading was System.DBNull.Value .
   Is it because of the driver I am using?? I have used MySQL-ODBC Bridge Version 3.51 for my application. I have executed the query "select * from jobschedules" in the MySQL Query Browser. It is working fine there.But the problem occured when I executed the following code -

static void Main(string[] args)
{
    OdbcConnection oConn =new OdbcConnection("DRIVER={MySQL ODBC 3.51 Driver};SERVER=delta;Database=afndbpdev;Uid=afndbp dev;Pwd=afndbpdev;");
    OdbcCommand oComm=new OdbcCommand();

    String tabname=String.Empty;
    oConn.Open();
    oComm.Connection=oConn;
    DataSet ds =new DataSet();

    oComm.CommandText="select * from jobschedule";
    OdbcDataAdapter oDA=new OdbcDataAdapter(oComm);
    // OdbcDataReader dr=null;
    try
    {
         oDA.FillSchema(ds,System.Data.SchemaType.Source);
         oDA.Fill(ds);
         // dr= oComm.ExecuteReader();
    }
    catch(Exception err)
    {
          Console.WriteLine(err.Message);
    }
   /*int i=0;
    while(dr.Read())
    {
    if(i>=dr.FieldCount)i=0;
    Console.WriteLine(dr.GetDataTypeName(i)+" "+dr.GetValue(i).ToString());

        i++;
    }*/
    foreach(DataRow r in ds.Tables[0].Rows)
       foreach(DataColumn c in ds.Tables[0].Columns )
       {
        Console.Write(c.DataType.ToString()+" ");
        Console.WriteLine(r[c].ToString());
        if(c.DataType.ToString().Equals("System.TimeSpan") )
        {
             Console.Write(" Value: "+ r[c].ToString());
           if(r[c]==System.DBNull.Value)
           Console.WriteLine("The Value is null");

        }
        }
}

    I am getting the values for all other fields from the code except the time field(i.e. scheduletime) . Here is the schema details for the table 'jobschedule' -

CREATE TABLE jobschedule
(
  jobid int(11) default NULL,
  assnnbr int(11) default NULL,
  emails text,
  jobtype varchar(255) default NULL,
  jobdescription varchar(255) default NULL,
  jobarguments varchar(255) default NULL,
  lastuserbatched datetime default NULL,
  lastautobatched datetime default NULL,
  scheduletype char(1) default NULL,
  scheduledate tinyint(4) default NULL,
  scheduletime time default NULL,
  enabled char(1) default NULL
)

  The output is as follows -
System.Int32 1
System.Int32 295
System.String [email protected]
System.String DownloadMembership
System.String Daily Nightly Downlad
System.String
System.DateTime 2/8/2005 2:28:31 PM
System.DateTime 10/24/2005 11:06:37 PM
System.String D
System.Int16 1
System.TimeSpan
   Value: The value is Null
System.String Y

        The content in the database was something like this (in the same order of the columns as discussed while creating the table)-
1, 295, '[email protected]', 'DownloadMembership', 'Daily Nightly Downlad', '', '2005-02-08 14:28:31', '2005-10-24 23:06:37', 'D', 1, 23:00:00, 'Y'.

   Hope it will help you to figure out the problem.
Bye
Spc


 
Old November 30th, 2005, 06:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

While creating the table if you keep the 'scheduletime' as datetime, your problem should be solved.

Later on you can optimise the retrival of data in the schema setting it up to retrieve only the time part
like this
<AttributeType name="scheduletime" dt:type="time" />

I would like see, though, if it could be solved without changing the design of the table. Do update the forum if you come up with some solution to it.

Regards
Ankur Verma
 
Old November 30th, 2005, 06:59 AM
spc spc is offline
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Ankur,
  Thanks for ur reply. I appreciate your idea but the datatype of the source table can't be altered. Otherwise it could have solved the problem very easily. Please think of some other alternatives. Meanwhile I am also trying to fix the problem.

with regards
Shubhabrata

 
Old August 6th, 2007, 01:33 PM
Registered User
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this:

string myTime = row["myTime"] != DBNull.Value ? Convert.ToDateTime(row["myTime"].ToString()).ToString("HH:mm") : "";






Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to connect PHP with Mysql bhavna MySQL 1 June 5th, 2007 07:52 PM
Unable to open ProjectTimeTracker DB cancer2006 BOOK: Beginning VB.NET Databases 2 October 24th, 2006 03:53 PM
Unable to import db The Beginner SQL Server 2000 9 August 24th, 2006 06:03 AM
Unable to read Time field value from MySQL DB spc SQL Language 4 November 30th, 2005 01:15 AM
Unable to INSERT INTO db nvillare Classic ASP Databases 6 August 26th, 2003 10:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.