none
Get time from db server in codefirst RRS feed

  • Question

  • Hi every body

    how to get time from db server and not system at codefirst?

    thanksfull.

    • Moved by CoolDadTx Wednesday, October 30, 2019 2:04 PM EF related
    Wednesday, October 30, 2019 12:12 PM

All replies

  • With Entity Framework Core code first add the following reference

    Microsoft.EntityFrameworkCore.Relational

    using (var context = new someContext())
    {
        using (var command = context.Database.GetDbConnection().CreateCommand())
        {
            command.CommandText = "SELECT SYSDATETIME();";
            command.CommandType = CommandType.Text;
            context.Database.OpenConnection();
            var result = (DateTime)command.ExecuteScalar();
            Console.WriteLine(result);
        }
    }
    Entity Framework 6 should be similar but will be different.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 30, 2019 1:59 PM
  • You're the best  Kareninstructor ,but i want to get the dbserver time, which is the time the time for the system on which the database it's on it and not clinet.
    • Edited by Ali_Lonbar Sunday, November 3, 2019 7:51 AM
    Sunday, November 3, 2019 7:00 AM
  • You're the best  Kareninstructor ,but i want to get the dbserver time, which is the time the time for the system on which the database it's on it and not clinet.

    There are several ways such as

    SELECT Convert(Time, GetDate());
    SELECT LTRIM(RIGHT(CONVERT(VARCHAR(50), GETDATE(), 100), 7));
    SELECT CONVERT(VARCHAR(8), GETDATE(), 108)
    SELECT RIGHT(CONVERT(VARCHAR(26), GETDATE(), 109),14)

    Or use my original reply and format the date time to just time. You can so remove ss to remove seconds and or remove tt for AM/PM.

    result.ToString("hh:mm:ss tt")


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, November 3, 2019 11:04 AM
  • Hi every body

    how to get time from db server and not system at codefirst?

    thanksfull.

    You would have to use the EF backdoor if using EF 6  and below using the SQL command objects, T-SQL statement and a datareader.

    https://blogs.msdn.microsoft.com/alexj/2009/11/07/tip-41-how-to-execute-t-sql-directly-against-the-database/


    Sunday, November 3, 2019 11:22 AM