473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Millisecond values missing when inserting datetime into datetime column of sql Server

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
SqlConnection connectionToDatabase = new SqlConnection("Data Source=.\
\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI");
connectionToDatabase.Open();
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");
}
When I run the above code, data is inserted into the table
The value in the date_t column is 2007-07-09 22:10:11 000.The
milliseconds value is always 000 only.I need the millisecond values
also in date_t column.
Is there any conversion needed for millisecond values?

Thanks,
Mani

Jul 9 '07 #1
4 5534
On Mon, 09 Jul 2007 15:13:52 -0700, Manikandan
<pl**********@gmail.comwrote:
>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
SqlConnection connectionToDatabase = new SqlConnection("Data Source=.\
\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI");
connectionToDatabase.Open();
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");
}
When I run the above code, data is inserted into the table
The value in the date_t column is 2007-07-09 22:10:11 000.The
milliseconds value is always 000 only.I need the millisecond values
also in date_t column.
Is there any conversion needed for millisecond values?

Thanks,
Mani
Try using a parameterized query, and let .NET and SQL server agree
precisely how to store the value of the datetime.

Calling DateTime.Now.ToString() will not return the millisecond
values. On my PC for instance it returns the following:

7/11/2007 9:45:34 PM

--
http://bytes.thinkersroom.com
Jul 11 '07 #2
Hi

is your time value generated outside the database or do you need it as a
verification when the record has been generated or modified? If it is the
latter one, use for insert the default value constraint of the database
field. For updates, you need a trigger on the table definition. SQL-Server
inserts the values with the milli second part.

If you need more help, please ask me directly on mail, I can send you some
sample code for SQL-Server

Regards

Roman
"Manikandan" <pl**********@gmail.comschrieb im Newsbeitrag
news:11*********************@w3g2000hsg.googlegrou ps.com...
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
SqlConnection connectionToDatabase = new SqlConnection("Data Source=.\
\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI");
connectionToDatabase.Open();
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");
}
When I run the above code, data is inserted into the table
The value in the date_t column is 2007-07-09 22:10:11 000.The
milliseconds value is always 000 only.I need the millisecond values
also in date_t column.
Is there any conversion needed for millisecond values?

Thanks,
Mani

Jul 16 '07 #3
Rad [Visual C# MVP] wrote:
On Mon, 09 Jul 2007 15:13:52 -0700, Manikandan
<pl**********@gmail.comwrote:
>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
SqlConnection connectionToDatabase = new SqlConnection("Data Source=.\
\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=SSPI");
connectionToDatabase.Open();
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");
}
When I run the above code, data is inserted into the table
The value in the date_t column is 2007-07-09 22:10:11 000.The
milliseconds value is always 000 only.I need the millisecond values
also in date_t column.
Is there any conversion needed for millisecond values?

Thanks,
Mani

Try using a parameterized query, and let .NET and SQL server agree
precisely how to store the value of the datetime.

Calling DateTime.Now.ToString() will not return the millisecond
values. On my PC for instance it returns the following:

7/11/2007 9:45:34 PM
Not by default - it will if you tell it to though, as per:
DateTime.Now.ToString("M/dd/yyyy H:mm:ss.fff tt")

-rick-
Jul 17 '07 #4
>>
Calling DateTime.Now.ToString() will not return the millisecond
values. On my PC for instance it returns the following:

7/11/2007 9:45:34 PM

Not by default - it will if you tell it to though, as per:
DateTime.Now.ToString("M/dd/yyyy H:mm:ss.fff tt")

-rick-
Which is what I'm saying. If you call the plain ToString() and pass
that, you lose the precision of the smaller units

--
http://bytes.thinkersroom.com
Jul 18 '07 #5

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

Similar topics

1
by: sgh | last post by:
Why doesn't this date time pattern support milliseconds? That makes it hard for me to map my date, time, and dateTime XML schema types to System.DateTime values without losing millisecond...
3
by: Art at ABE Computer Consultants | last post by:
I'm missing something really simple, I'm sure. I have a form to be filled in. The operator choosed a plant species (the botanical name) from a list in a combo box. (The lookup table is populated...
6
by: Dean Slindee | last post by:
I am looking for the "right" way to handle inserting and presenting null date values. Public Const c_NullDate As Date = #12:00:00 AM# If I set the value of a date variable in an SQL Server insert...
2
by: Jonesgj | last post by:
Hi, I have connected to a SQL Server table using dataAdapter/Dataset to fill a data grid. On SQL Server a column is a DateTime datatype (its actually a 'LastLogon' column ) and has both Date and...
3
by: aling | last post by:
Execute following T-SQL within Queary Analyzer of SQL Server 2000: ======================================= DECLARE @dTest DATETIME SET @dTest='2001-1-1 1:1:1:991' SELECT @dTest SET...
19
by: RP | last post by:
I have a DateTimePicker with format dd-MM-yyyy. While attempting to insert this date in SQL Server Date column, following exception is thrown: The conversion of a char data type to a datetime...
3
by: hellboss | last post by:
Hi Everyone ! im using SQL SERVER 2005, My problem is, i am inserting a set of values into a table using a subquery, the problem occurs when i tried to get the auto generated column in the table...
1
by: arunbojan | last post by:
Hi friends, Im using a tabel with column names (timefrom, timeto, extension) .For this tabel im inserting the values using stored procedure... how to insert values if extension has range of...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.