Re: Insert Date

From: Miha Markic [MVP C#] (miha)
Date: 03/19/04


Date: Fri, 19 Mar 2004 09:32:11 +0100

Hi,

To be on the safe side, you should use parametrised sql insert command.
Something like:
SqlCommand cmd = new SqlCommand("INSERT INTO Table (..., datecolumn) VALUES
(@mydate)", ...)
cmd.Parameters.Add ("@mydate", SqlDbType.DateTime);

You should convert your date to DateTime type:
DateTime mydate = new DateTime(Int32.Parse(datestring.Substring(4, 4)),
Int32.Parse(datestring.Substring(2, 2)), Int32.Parse(datestring.Substring(0,
2)));
And prior to executing the command you should set the data:
cmd.Parameters["@mydate"].Value = mydate; // DateTime instance

Note, that I didn't test the code (typos possible :) )

-- 
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Chris" <anonymous@discussions.microsoft.com> wrote in message
news:7D1EB38E-0AC9-405C-96EB-BB8B59548867@microsoft.com...
> Hi,
> I have a column with data type datetime. How do I insert this 03042004?


Relevant Pages

  • Re: Dates
    ... Create SqlParameter objects for the parameters and add them to the Parameters collecion of the command. ... The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. ... System.Data.Common.DbDataAdapter.FillFromReader(Object data, String srcTable, IDataReader dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue) +260 ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Console application switches for argument values?
    ... On the DEBUG tab in the text box labeled "Command line arguments:" enter ... DateTime end = DateTime.Now; ... bool good = true; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Count Time hours, minutes, seconds
    ... you can use the DATETIME() function. ... command to stop the time then keep adding this time to a total time ... The idea is to add the time that a user types ... into the memo field in order to keep track of how long they are ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Variable substitution to replace switch
    ... function addtime ... // How to add time to $datetime (data type datetime) and return it as ... but I would like to replace the switch statement with one ...
    (comp.lang.php)
  • Re: File.mtime <=> DateTime.strptime()
    ... is a DateTime, ... require 'alib' ... main programs that support usage, logging, command line and option parsing in seconds ...
    (comp.lang.ruby)