473,651 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting the datetime with milliseconds value into a datarow

Hi,
I need to insert the datetime with milliseconds value into a datarow.

My code as below
DataTable testDataTable=n ew DataTable();

testDataTable.C olumns.Add("upd atedDateTime", typeof(DateTime ));

DataRow testDateRow=sur veyUpdatedDateD ataTable.NewRow ();
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.R ows.Add(surveyU pdateDateRow);

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

for(int testRows=0;test Rows<testDataTa ble.Rows.Count; testRows++)
{

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

}

Any conversion required?
When i give a messagebox for DateTime.Now.To String("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

Jul 9 '07 #1
6 30013
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?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Manikandan " <pl**********@g mail.comwrote in message
news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
Hi,
I need to insert the datetime with milliseconds value into a datarow.

My code as below
DataTable testDataTable=n ew DataTable();

testDataTable.C olumns.Add("upd atedDateTime", typeof(DateTime ));

DataRow testDateRow=sur veyUpdatedDateD ataTable.NewRow ();
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.R ows.Add(surveyU pdateDateRow);

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

for(int testRows=0;test Rows<testDataTa ble.Rows.Count; testRows++)
{

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

}

Any conversion required?
When i give a messagebox for DateTime.Now.To String("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

Jul 9 '07 #2
On 9 Jul, 18:08, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
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?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard .caspershouse.c om

"Manikandan " <plmanikan...@g mail.comwrote in message

news:11******** **************@ q75g2000hsh.goo glegroups.com.. .
Hi,
I need to insert the datetime with milliseconds value into a datarow.
My code as below
DataTable testDataTable=n ew DataTable();
testDataTable.C olumns.Add("upd atedDateTime", typeof(DateTime ));
DataRow testDateRow=sur veyUpdatedDateD ataTable.NewRow ();
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.R ows.Add(surveyU pdateDateRow);
when I read the values back using the following code I lose the
millisecond value
for(int testRows=0;test Rows<testDataTa ble.Rows.Count; testRows++)
{
Messagebox.Show (testDataTable. Rows[testRows]
["updatedDateTim e"] .ToString()
}
Any conversion required?
When i give a messagebox for DateTime.Now.To String("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
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

Jul 9 '07 #3
On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan
<pl**********@g mail.comwrote:
>
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff") ;
testDataTable. Rows.Add(survey UpdateDateRow);
Why don't you do this directly?

testDateRow["updatedDateTim e"]=DateTime.Now

--
http://bytes.thinkersroom.com
Jul 9 '07 #4
On 9 Jul, 21:04, "Rad [Visual C# MVP]" <r...@nospam.co mwrote:
On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan

<plmanikan...@g mail.comwrote:
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.R ows.Add(surveyU pdateDateRow);

Why don't you do this directly?

testDateRow["updatedDateTim e"]=DateTime.Now

--http://bytes.thinkersr oom.com
Hi,
I tried datetime.now.
The millisecond value is 000 for datetime.now.
Then only I used DateTime.Now.To String("yyyy-MM-dd
HH:mm:ss.fff");
Thanks
Mani

Jul 9 '07 #5
On 9 Jul, 21:04, "Rad [Visual C# MVP]" <r...@nospam.co mwrote:
On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan

<plmanikan...@g mail.comwrote:
testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff");
testDataTable.R ows.Add(surveyU pdateDateRow);

Why don't you do this directly?

testDateRow["updatedDateTim e"]=DateTime.Now

--http://bytes.thinkersr oom.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(Sy stem.Int16));
dt1.Columns.Add ("date_t", typeof(System.D ateTime));
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)dt 1.Rows[i]["date_t"];
string insertQuery = "insert into date_test values(" +
str + ",'" + dt + "')";
SqlCommand cmd = new SqlCommand(inse rtQuery,
connectionToDat abase);
cmd.ExecuteNonQ uery();
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

Jul 9 '07 #6
"Manikandan " <pl**********@g mail.comwrote in message
news:11******** **************@ g4g2000hsf.goog legroups.com...
On 9 Jul, 21:04, "Rad [Visual C# MVP]" <r...@nospam.co mwrote:
>On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan

<plmanikan...@ gmail.comwrote:
>testDateRow["updatedDateTim e"]=DateTime.Now.T oString("yyyy-MM-dd
HH:mm:ss.fff") ;
testDataTable. Rows.Add(survey UpdateDateRow);

Why don't you do this directly?

testDateRow["updatedDateTim e"]=DateTime.Now

--http://bytes.thinkersr oom.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(Sy stem.Int16));
dt1.Columns.Add ("date_t", typeof(System.D ateTime));
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)dt 1.Rows[i]["date_t"];
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/lib...2(sql.80).aspx for more
information.

SqlCommand cmd = new SqlCommand(inse rtQuery,
connectionToDat abase);
cmd.ExecuteNonQ uery();
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
Jul 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
58197
by: Dave Pylatuk | last post by:
Hello all. I am attempting to insert a row into a table with a datetime column: When the insert statement contains a value for the millisecond portion of the data time column: ie. {ts '2003-11-05 12:02:43:2960'} I get 'Syntax error converting datetime from string' When I insert a value like: {ts '2003-11-05 12:02:43'}
3
12006
by: Matthias S. | last post by:
Hi, I create a DateTime object in C# like this... DateTime _DateStart = new DateTime(2000, 1, 1, 1, 1, 1, 1); Now I save it to the Database (into a DateTime type of column) using the SqlCommand and later I reload the value. Comparing those using the == operator of the DateTime object yields incorrect results. Say I have two DateTime objects, one was loaded from the DB:
6
14966
by: Paulb1us | last post by:
I want to set a DateTime field to Null before passing it to the DB //First I check to see if anything is in this datarow column, because sometimes we have no data. DateTime dt; if ( datarow.ToString().Length > 0) { //We assume the data is a date dt = (DateTime)datarow; }
10
11609
by: Ryan Graham | last post by:
I totally bombed this question in an interview so I'm posting my answer here for comments and suggestions... perhaps (god help me) I'm just not that bright, but this works and seems to be fairly efficent. The idea was simple, insert an integer into a list that has already been sorted. private int _list; .... public void Insert(int value) { int tempArray;
4
3665
by: sumip | last post by:
I just made a pretty strange observation with .NET 2.0 - DateTime.AddMilliseconds(double) and AddSeconds(double) seem to be broken: DateTime now = new DateTime(); DateTime t2 = now.AddMilliseconds(1.5); Console.Out.WriteLine(t2.TimeOfDay); The code above rounds the fraction to full milliseconds and results in this output:
3
28493
by: Mark | last post by:
I'd like to compare two datetime values in milliseconds. The datetime.compare method appears to show only seconds. Milliseconds of a datetime are available as a property of each datetime, but I am assuming there is an easier way than comparing the milliseconds, seconds, minutes, hours, days, etc, individually to generate the difference. Thanks in advance. Mark
2
8228
by: vikram.mankar | last post by:
I'm running into a constant issue of SQL Server modifying the millisecond part of a timestamp insert from another application. The application inserts timestamp which includes a millisecond portion as a string (varchar). But when an SQL Server moves this data to another table (for reporting), the string is inserted in a datetime field, the millisecond field invariably changes by 1-2 milliseconds for every single data point inserted. Given...
4
5578
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime C# coding
0
2801
by: maolimix | last post by:
Hi to all, I have a trouble with datetime...now I explain.. I must to store to a C# variable the values from a "Datetime column" of Sql Server 2005. I tried 3 methods ...in the first two I never was able to get the Millisecond from datacolumn. I explain the methods... 1) I created a Datatable that contain the column... then I fill a datarow with the current value. The system automatically converts the Sqldatetime in System.Datetime but...
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8803
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.