|
-
Apr 18th, 2007, 02:50 AM
#1
Thread Starter
Hyperactive Member
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...
-
Apr 18th, 2007, 03:10 AM
#2
Re: data and time
Firstly, how is this:
vb Code:
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: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.
-
Apr 18th, 2007, 03:23 AM
#3
Thread Starter
Hyperactive Member
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
-
Apr 18th, 2007, 04:02 AM
#4
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:
comm.CommandText = "INSERT INTO tblData(..., DateTime, ...) VALUES (..., @DateTime, ...)";
comm.Parameters.AddWithValue("@DateTime", DateTime.Now);
-
Apr 18th, 2007, 09:34 PM
#5
Thread Starter
Hyperactive Member
Re: data and time
tnx to you jmcilhinney,
this one works...
insert into tblData(...DATE_TIME) values(..., #"+ DateTime.Now+"#);
-
Apr 18th, 2007, 09:39 PM
#6
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.
-
Apr 18th, 2007, 10:01 PM
#7
Thread Starter
Hyperactive Member
Re: data and time
another question...
how can i save data from access to excel or csv file?
-
Apr 24th, 2007, 06:23 AM
#8
Re: data and time
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|