Main Page | Report this Page
Computers Forum Index  »  Computer - Databases - MS SQL Server  »  DateTime type in SQL Server 2008...
Page 1 of 1    

DateTime type in SQL Server 2008...

Author Message
yqever...
Posted: Mon Sep 28, 2009 5:25 am
Guest
TEMPLATES table has a field X_UPDATED whose type is datetime. I use the code
to update it in SQL Server 2008 but an exception occurs.


cmd = new OdbcCommand("UPDATE TEMPLATES SET X_UPDATED=?
WHERE FILENAME='" + fileName + "'", this._dbConn);

OdbcParameter param2 = cmd.Parameters.Add("X_UPDATED",
OdbcType.DateTime);

param2.Value = DateTime.Now; // Exception
cmd.ExecuteNonQuery();



The exception is : System.Data.Odbc.OdbcException: ERROR [22008]
[Microsoft][SQL Server Native Client 10.0]Datetime field overflow.
Fractional second precision exceeds the scale specified in the parameter
binding.

It looks like the OdbcType.DateTime doesn't match SQL Server 2008's
datetime. How should I change my code to update the datetime field in
sqlserver 2008? Thanks.

BTW, sql server 2005 doesn't throw this exception.
 
Erland Sommarskog...
Posted: Mon Sep 28, 2009 8:25 am
Guest
yqever (yqever at (no spam) 163.com) writes:
Quote:
TEMPLATES table has a field X_UPDATED whose type is datetime. I use the
code to update it in SQL Server 2008 but an exception occurs.


cmd = new OdbcCommand("UPDATE TEMPLATES SET X_UPDATED=?
WHERE FILENAME='" + fileName + "'", this._dbConn);

OdbcParameter param2 = cmd.Parameters.Add("X_UPDATED",
OdbcType.DateTime);

param2.Value = DateTime.Now; // Exception
cmd.ExecuteNonQuery();



The exception is : System.Data.Odbc.OdbcException: ERROR [22008]
[Microsoft][SQL Server Native Client 10.0]Datetime field overflow.
Fractional second precision exceeds the scale specified in the parameter
binding.

It looks like the OdbcType.DateTime doesn't match SQL Server 2008's
datetime. How should I change my code to update the datetime field in
sqlserver 2008? Thanks.

BTW, sql server 2005 doesn't throw this exception.

Correct, they did some changes there. I ran into the same thing with
OLE DB. If memory serves the issue is that you cannot provide a value
with more than three decimals for the fractional value of a datetime
value in SQL Server, as datetime has a precision of 3.33 ms. So if
DateTime.Now includs microseonds you get this error. You would need
to strip these first.

Two other comments:

1) Why do you inline fileName in the query? You should make it a parameter
at all.

2) Why do you use Odbc Client to access SQL Server? OK, if your application
also can interact with other platforms, there maybe reason for it. But
if you access SQL Server only, you should use SqlClient instead.






--
Erland Sommarskog, SQL Server MVP, esquel at (no spam) sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 
yqever...
Posted: Mon Sep 28, 2009 1:05 pm
Guest
Thank you, Erland. I truncated the millisenconds of updateTime. No
cexeption occurs.

My code needs to work with MS Access, SQL Server and Oracle. So I can't use
SqlClient.


"Erland Sommarskog" <esquel at (no spam) sommarskog.se> wrote in message
news:Xns9C946A1377FC7Yazorman at (no spam) 127.0.0.1...
Quote:
yqever (yqever at (no spam) 163.com) writes:
TEMPLATES table has a field X_UPDATED whose type is datetime. I use the
code to update it in SQL Server 2008 but an exception occurs.


cmd = new OdbcCommand("UPDATE TEMPLATES SET X_UPDATED=?
WHERE FILENAME='" + fileName + "'", this._dbConn);

OdbcParameter param2 = cmd.Parameters.Add("X_UPDATED",
OdbcType.DateTime);

param2.Value = DateTime.Now; // Exception
cmd.ExecuteNonQuery();



The exception is : System.Data.Odbc.OdbcException: ERROR [22008]
[Microsoft][SQL Server Native Client 10.0]Datetime field overflow.
Fractional second precision exceeds the scale specified in the parameter
binding.

It looks like the OdbcType.DateTime doesn't match SQL Server 2008's
datetime. How should I change my code to update the datetime field in
sqlserver 2008? Thanks.

BTW, sql server 2005 doesn't throw this exception.

Correct, they did some changes there. I ran into the same thing with
OLE DB. If memory serves the issue is that you cannot provide a value
with more than three decimals for the fractional value of a datetime
value in SQL Server, as datetime has a precision of 3.33 ms. So if
DateTime.Now includs microseonds you get this error. You would need
to strip these first.

Two other comments:

1) Why do you inline fileName in the query? You should make it a parameter
at all.

2) Why do you use Odbc Client to access SQL Server? OK, if your
application
also can interact with other platforms, there maybe reason for it. But
if you access SQL Server only, you should use SqlClient instead.






--
Erland Sommarskog, SQL Server MVP, esquel at (no spam) sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 
Erland Sommarskog...
Posted: Mon Sep 28, 2009 9:32 pm
Guest
yqever (yqever at (no spam) 163.com) writes:
Quote:
My code needs to work with MS Access, SQL Server and Oracle. So I can't
use SqlClient.

OK. My experience is that OleDb client works better with SQL Server than
Odbc Client, but maybe it's the other way round with the other two.



--
Erland Sommarskog, SQL Server MVP, esquel at (no spam) sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 
 
Page 1 of 1    
All times are GMT
The time now is Tue Dec 15, 2009 9:54 am