Friday, February 24, 2012

Date Datatype

I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to
insert the date and time into a SQL database by using hour(now). I am having
a hard time trying to figure out which datatype to use in SQL to store this
value. I have tried using datetime, char, nchar, text and nothing seems to
work. Anyone have any ideas? Thanks!
Regards, :)
Christopher BowenDo you want to store the time in terms of hours only?
Madhivanan|||Christopher,
Is the datatype of the column a DateTime field and make sure the variable
storing this value (in .NET i assume) is a System.DateTime - or use
System.DateTime.Now. Using the SqlCommand Object add a parameter of type
datetime and assign this .net variable eg:
public void InsertDateTime()
{
DB Access setup....
...
using (SqlCommand command = new SqlCommand(etc...));
{
command.Connection = someConnectionPreviouslySetup;
command.CommandText = "INSERT INTO TableToInsertInto
(DateTimeField) VALUES (@.DateTimeField)";
command.CommentType = CommandType.Text;
//add the parameter to the parameters collection
command.Parameters.Add("@.DateTimeField", SqlDbType.DateTime);
//set the parameter to the required value
command.Parameters["@.DateTimeField"].Value =
System.DateTime.Now;
command.ExecuteNonQuery();
}
}
I am doing a mini sample in c# (this will NOT compile).
Regards,
Adrian.
<madhivanan2001@.gmail.com> wrote in message
news:1109141955.543915.123420@.g14g2000cwa.googlegroups.com...
> Do you want to store the time in terms of hours only?
> Madhivanan
>|||Hi
Format your value as 'YYYYMMDD' and try to insert
"Christopher Bowen" <c_bowen@.earthlink.net> wrote in message
news:%23OdiaKXGFHA.1740@.TK2MSFTNGP09.phx.gbl...
> I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to
> insert the date and time into a SQL database by using hour(now). I am
having
> a hard time trying to figure out which datatype to use in SQL to store
this
> value. I have tried using datetime, char, nchar, text and nothing seems to
> work. Anyone have any ideas? Thanks!
> Regards, :)
> Christopher Bowen
>|||I will give your examples a try and I will let you know how it turns out.
Thanks for the help!
"Christopher Bowen" <c_bowen@.earthlink.net> wrote in message
news:#OdiaKXGFHA.1740@.TK2MSFTNGP09.phx.gbl...
> I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to
> insert the date and time into a SQL database by using hour(now). I am
having
> a hard time trying to figure out which datatype to use in SQL to store
this
> value. I have tried using datetime, char, nchar, text and nothing seems to
> work. Anyone have any ideas? Thanks!
> Regards, :)
> Christopher Bowen
>|||It worked! I went into my table and changed the datatype to "datetime" and
used the code system.datetime.now. Thanks to all of you for all of your
help!
Kind Regards, :)
Christopher Bowen
"Christopher Bowen" <c_bowen@.earthlink.net> wrote in message
news:%23OdiaKXGFHA.1740@.TK2MSFTNGP09.phx.gbl...
>I am using Visual Studio .NET 2003 with SQL Server 2000. I am trying to
>insert the date and time into a SQL database by using hour(now). I am
>having a hard time trying to figure out which datatype to use in SQL to
>store this value. I have tried using datetime, char, nchar, text and
>nothing seems to work. Anyone have any ideas? Thanks!
> Regards, :)
> Christopher Bowen
>

No comments:

Post a Comment