Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Inserting the datetime with milliseconds value into a datarow

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Manikandan - 09 Jul 2007 17:57 GMT
Hi,
I need to insert the datetime with milliseconds value into a datarow.

My code as below
DataTable testDataTable=new DataTable();

testDataTable.Columns.Add("updatedDateTime", typeof(DateTime));

DataRow testDateRow=surveyUpdatedDateDataTable.NewRow();

testDateRow["updatedDateTime"]=DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.Rows.Add(surveyUpdateDateRow);

when I read the values back using the following code I lose the
millisecond value

for(int testRows=0;testRows<testDataTable.Rows.Count;testRows++)
            {

Messagebox.Show(testDataTable.Rows[testRows]
["updatedDateTime"]    .ToString()

                        }

Any conversion required?
When i give a messagebox for DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss.fff") it is coming properly with milliseconds
I need the datetime values with milliseconds from datatable to insert
into sql server datetime column

Thanks,
Mani
Nicholas Paldino [.NET/C# MVP] - 09 Jul 2007 18:07 GMT
Mani,

   Why are you calling ToString on the DateTime?  Why not just set the
value of the row to the DateTime instance.  It should work then, and assign
your datetime appropriately.

   Also, are you sure you are using a datetime column and not a
smalldatetime column?

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi,
> I need to insert the datetime with milliseconds value into a datarow.
[quoted text clipped - 29 lines]
> Thanks,
> Mani
Manikandan - 09 Jul 2007 19:39 GMT
On 9 Jul, 18:08, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> Mani,
>
[quoted text clipped - 42 lines]
> > Thanks,
> > Mani

Hi,
When I assign the datetime, and updating that value into sql server
database table, Milliseconds are set to 000 only
I need to insert that datarow values into a sql server table
column(dat type:datetime)
Could you please tell me how to insert the datetime value with
millisecond from c# to sql server

Thanks,
Mani
Rad [Visual C# MVP] - 09 Jul 2007 21:04 GMT
>testDateRow["updatedDateTime"]=DateTime.Now.ToString("yyyy-MM-dd
>HH:mm:ss.fff");
>testDataTable.Rows.Add(surveyUpdateDateRow);

Why don't you do this directly?

testDateRow["updatedDateTime"]=DateTime.Now

--
http://bytes.thinkersroom.com
Manikandan - 09 Jul 2007 22:30 GMT
> On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan
>
[quoted text clipped - 9 lines]
>
> --http://bytes.thinkersroom.com

Hi,
I tried datetime.now.
The millisecond value is 000 for datetime.now.
Then only I used DateTime.Now.ToString("yyyy-MM-dd
> >HH:mm:ss.fff");

Thanks
Mani
Manikandan - 09 Jul 2007 22:53 GMT
> On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan
>
[quoted text clipped - 9 lines]
>
> --http://bytes.thinkersroom.com

Hi,
I have table in sql server 2000
Table name:date_test
Column name  datatype
no                   int
date_t             DateTime
I tried the following code
DataTable dt1 = new DataTable();
           dt1.Columns.Add("no",typeof(System.Int16));
           dt1.Columns.Add("date_t", typeof(System.DateTime));
           DataRow dr = dt1.NewRow();
           dr["no"] = 1;
           dr["date_t"] = DateTime.Now;
           dt1.Rows.Add(dr);
           for(int i=0;i<dt1.Rows.Count;i++)
           {
               string str=dt1.Rows[i]["no"].ToString();
               DateTime dt=(DateTime)dt1.Rows[i]["date_t"];
               string insertQuery = "insert into date_test values(" +
str + ",'" + dt + "')";
               SqlCommand cmd = new SqlCommand(insertQuery,
connectionToDatabase);
               cmd.ExecuteNonQuery();
               MessageBox.Show("saved");
           }
After inserting the record from C#, I checked the table in sql server,
the milliseconds are stored with 000 i.e(2007-07-09 21:31:32 000)
I want the datetime value with milliseconds
How to insert a datetime value into sql server?

Thanks,
Mani
John Vottero - 09 Jul 2007 23:29 GMT
>> On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan
>>
[quoted text clipped - 30 lines]
>                string insertQuery = "insert into date_test values(" +
> str + ",'" + dt + "')";

You're losing the milliseconds here because you're converting the Datetime
into a string.  Pass the Datetime to SQL using a SqlParameter and
parameterized SQL.  Add the paramters to the SqlCommand with code like:

          cmd.Parameters.Add(new SqlParameter("@dtName", dt));

Then, refer to the "@dtName" parameter in your Sql like:

          string insertQuery = "insert into date_test values(@strName,
@dtName)";

See http://msdn2.microsoft.com/en-us/library/aa175652(sql.80).aspx for more
information.

>                SqlCommand cmd = new SqlCommand(insertQuery,
> connectionToDatabase);
[quoted text clipped - 8 lines]
> Thanks,
> Mani

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.