I want the reason why C# date is bigger than SQL Date even if C# code is running first and after that SQL query,
Logically the SQL date should be greater than C# date.
For your reference the .NET application and SQL Server are on my local machine.
C# Code:
using System.Data;
using System.Data.SqlClient;
for (int i = 1; i <= 20; i++)
{
AddRecord();
}
Console.WriteLine("200 records added in database....");
void AddRecord()
{
try
{
string ConnectionString = @"data source=OM5SQL2019; database=TestDb; integrated security=SSPI";
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
SqlCommand cmd = new SqlCommand()
{
CommandText = "SP_AddRecord",
Connection = connection,
CommandType = CommandType.StoredProcedure
};
DateTime date = DateTime.UtcNow;
SqlParameter param1 = new SqlParameter
{
ParameterName = "@ReceivedOn",
SqlDbType = SqlDbType.DateTime,
Value = date,
Direction = ParameterDirection.Input
};
cmd.Parameters.Add(param1);
connection.Open();
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception Occurred: {ex.Message}");
}
}
SQL Procedure:
CREATE OR ALTER PROCEDURE [dbo].[SP_AddRecord]
@ReceivedOn datetime
AS
BEGIN
SET NOCOUNT ON;
insert into Records(CreateDate,ReceivedOnDate) values
(GETUTCDATE(),@ReceivedOn)
END
Result in table
Id | ReceivedOnDate | CreateDate | Difference in MS |
---|---|---|---|
1 | 2024-07-25 10:55:47.640 | 2024-07-25 10:55:49.800 | 2160 |
2 | 2024-07-25 10:55:49.873 | 2024-07-25 10:55:49.870 | -3 |
3 | 2024-07-25 10:55:49.877 | 2024-07-25 10:55:49.900 | 24 |
4 | 2024-07-25 10:55:49.903 | 2024-07-25 10:55:49.913 | 10 |
5 | 2024-07-25 10:55:49.917 | 2024-07-25 10:55:49.917 | 0 |
6 | 2024-07-25 10:55:49.920 | 2024-07-25 10:55:49.917 | -4 |
7 | 2024-07-25 10:55:49.920 | 2024-07-25 10:55:49.917 | -4 |
8 | 2024-07-25 10:55:49.920 | 2024-07-25 10:55:49.920 | 0 |
9 | 2024-07-25 10:55:49.920 | 2024-07-25 10:55:49.920 | 0 |
10 | 2024-07-25 10:55:49.920 | 2024-07-25 10:55:49.920 | 0 |
11 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
12 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
13 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
14 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
15 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
16 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.920 | -3 |
17 | 2024-07-25 10:55:49.923 | 2024-07-25 10:55:49.923 | 0 |
18 | 2024-07-25 10:55:49.927 | 2024-07-25 10:55:49.923 | -3 |
19 | 2024-07-25 10:55:49.927 | 2024-07-25 10:55:49.923 | -3 |
20 | 2024-07-25 10:55:49.927 | 2024-07-25 10:55:49.923 | -3 |
I have tried many methods
- In C# – DateTime.Now & SQL – GETDATE()
- In C# – DateTime.Now & SQL – SYSDATETIME()
- In C# – DateTime.UTCNow & SQL – GETUTCDATE()
- In C# – DateTime.UTCNow & SQL – SYSUTCDATETIME()
Also changed column type from datetime
to datetime2
, varchar(max)
.
I want an actual reason or an authentic source which can explain this.
Shivam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3