Results 1 to 8 of 8

Thread: data and time

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    data and time

    MyDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
    dtDay = MyDate.ToString("MM/dd/yyyy");
    dtTime= MyDate.ToString("hh:mm:ss tt");

    comm.CommandText = "INSERT INTO tblData(DTE,TME) VALUES(#"dtDay"#,#"dtTime"#)";

    i want to separate date and time in the database
    i have a message data type mismatch

    anyone can solve this?

    tnx...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: data and time

    Firstly, how is this:
    vb Code:
    1. MyDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
    an advantage over this:
    vb Code:
    1. MyDate = DateTime.Now;
    As to your issue, what database are you using and what types are your columns? I'll wager that if you used parameters, as everyone should, then you wouldn't have a problem. My guess is that both your columns are date/time and you'll simply be storing the same value in both columns anyway. Just because you format a column a certain way doesn't mean you're changing the way the data is stored. If this is an Access database then the format only affects how the values are displayed, not how they're stored. In that case you should just save the data in one column.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: data and time

    i saw this in a tutorial...
    and learned from you that both MyDate are the same

    MyDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);

    MyDate = DateTime.Now;

    using access database, ok i already delete the DTE and TME columns
    i create new DATE_TIME type as general date

    insert into tblData(..., DATE_TIME) VALUES(...,#" + MyDate + "#)

    but it has still error data type mismatch

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: data and time

    Many beginner tutorials will show, and various tutors will recommend, using string concatenation to build SQL statements. It does more harm than good and this is just one of the many examples. All books and teaching material should just do it the proper way from the start, and the proper way is with parameters:
    C# Code:
    1. comm.CommandText = "INSERT INTO tblData(..., DateTime, ...) VALUES (..., @DateTime, ...)";
    2. comm.Parameters.AddWithValue("@DateTime", DateTime.Now);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: data and time

    tnx to you jmcilhinney,

    this one works...
    insert into tblData(...DATE_TIME) values(..., #"+ DateTime.Now+"#);

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: data and time

    If you're not going to use parameters to insert values into SQL statements then you're just asking to have more issues down the track.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: data and time

    another question...
    how can i save data from access to excel or csv file?

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: data and time

    Quote Originally Posted by basti42
    another question...
    how can i save data from access to excel or csv file?
    This might help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width