468,306 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,306 developers. It's quick & easy.

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=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;testR ows++)
{

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

Jul 9 '07 #1
6 29157
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.com

"Manikandan" <pl**********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
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;testR ows++)
{

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

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

"Manikandan" <plmanikan...@gmail.comwrote in message

news:11**********************@q75g2000hsh.googlegr oups.com...
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;testR ows++)
{
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
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**********@gmail.comwrote:
>
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
Jul 9 '07 #4
On 9 Jul, 21:04, "Rad [Visual C# MVP]" <r...@nospam.comwrote:
On Mon, 09 Jul 2007 09:57:14 -0700, Manikandan

<plmanikan...@gmail.comwrote:
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
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

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

<plmanikan...@gmail.comwrote:
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
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

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

<plmanikan...@gmail.comwrote:
>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

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 + "')";
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(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
Jul 9 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Matthias S. | last post: by
6 posts views Thread by Paulb1us | last post: by
10 posts views Thread by Ryan Graham | last post: by
3 posts views Thread by Mark | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.