Error converting varchar to datetime data type when starting raw SQL, but not when LINQ to SQL is called

The client runs my code on error. They sent me SQL from profilder.

When I paste it into SQL Server Management Studio, it fails: Error converting varchar data type to datetime

However, this will not work when I run it in my local dev block or another server.

To test, I created a simple application with an L2S data file containing a single object that looks something like this:

public class UserAccount 
{
    public int Id { get; set; }
    public string Username { get; set; }
    public DateTime? LastActivity { get; set; }
}

Insert a record and then update it:

var account = db.UserAccounts.Single(a => a.Username == "Mojo");
account.LastActivity = DateTime.Now;
db.SubmitChanges();

Records are updated in the database. But when I get T-SQL from Profiler:

exec sp_executesql N'UPDATE [UserAccount] SET [LastActivity] = @p2 WHERE ([Id] = @p0) AND ([Username] = @p1) AND ([LastActivity] IS NULL)',N'@p0 int,@p1 nvarchar(4),@p2 datetime',@p0=1,@p1=N'Mojo',@p2='2009-11-10 14:04:41.7470000' 

and execute it in SQL Server Management Studio, I get: Error converting varchar data type to datetime

What am I missing?

+3
5

Management Studio, ? , , , -.

+1

:

'2009-11-10 14:04:41.747' (works)
'2009-11-10 14:04:41.7470000' (error converting...)

, , Visual Studio. LINQ SQL Server.

+3

, SQL Server?

datetime2, 2008 , 2005 .

+1

DATETIMEshould have only 3 digits after the second, DATETIME2has 7 digits after the second. It seems the string is formatted for DATETIME2.

+1
source

I got this error as soon as I tried to insert the date when "1/1/0001 12:00:00 AM"

0
source

Source: https://habr.com/ru/post/1722408/


All Articles