Question: I want to get SQL Server's DateTime in this format `2020-04-29 08:31:50.813`

This is my Code: 

```
    [WebMethod]
    public void getDTOServer()
    {
        string sqlStr = string.Format(@"use [{0}] 
            SELECT GETDATE() AS 'DTOServer'
        ", DatabaseName);
        SqlCommand cmd = new SqlCommand(sqlStr);

        string DT = Man.getSqlDataset(cmd).Tables[0].Rows[0][0].ToString();

        Context.Response.Clear();
        Context.Response.Flush();
        Context.Response.Write(DT);
    }

```

The problem is when I'm running this query `(SELECT GETDATE() AS 'DTOServer')` in SQL Server I get correct output but C# gives me output like this `29-Apr-20 8:31:51 AM`.

What I want is `2020-04-29 08:31:50.813`.

Answer: 
You can customize the `ToString` method of a proper `DateTime` object

```
ToString("yyyy-MM-dd hh:mm:ss.fff");

```
Just got a solution. Convert `DateTime` to `VARCHAR`

Like This:

```
        [WebMethod]
        public void getDTOServer()
        {
            string sqlStr = string.Format(@"use [{0}] 
                SELECT REPLACE(CONVERT(VARCHAR, GETDATE(), 126), 'T', ' ') AS 'DTOServer'
            ", DatabaseName);
            SqlCommand cmd = new SqlCommand(sqlStr);

            string DT = Man.getSqlDataset(cmd).Tables[0].Rows[0][0].ToString();

            Context.Response.Clear();
            Context.Response.Flush();
            Context.Response.Write(DT);
        }

```

The issue was C# was converting it to local format because the data type was DateTime