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