Inserting the datetime with milliseconds value into a datarow

M

Manikandan

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
M

Manikandan

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]
- (e-mail address removed)


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

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
 
R

Rad [Visual C# MVP]

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
 
M

Manikandan

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
Thanks
Mani
 
M

Manikandan

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["no"].ToString();
DateTime dt=(DateTime)dt1.Rows["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
 
J

John Vottero

Manikandan said:
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["no"].ToString();
DateTime dt=(DateTime)dt1.Rows["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/library/aa175652(sql.80).aspx for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top