Extract date as String Sqlserver C #

1

When I run a query on sql server from C # the fields of the date type are extracted as Date(1445580000000) when formatted in JSON. Although in the query specify the CONVERT to do so in yyyy-mm-dd , the same thing happened to me once in php and the connection specified something like GetDateAsString=>true for solve it.

It is possible to define that in the connection or in the sqlCommand or DataReader , or I have to go through the data to convert the date manually, and then go back through it to turn it into JSON . What will consume me more resources, since they are more than 10 thousand rows . Any ideas?

 DataTable dt = new DataTable();
            SqlConnection sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["CIS"].ConnectionString);
            string select = "EXECUTE dbo.getInforme";
            sqlConnection.Open();
            SqlDataAdapter da = new SqlDataAdapter(select, sqlConnection);
            da.Fill(dt);

 System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dt.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dt.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                rows.Add(row);
            }

            sqlConnection.Close();
            serializer.MaxJsonLength = Int32.MaxValue;
            Response.Write(serializer.Serialize(rows));

Note: dbo.getReport is a stored procedure that returns this information

    
asked by Aquiles Perez 27.10.2016 в 01:28
source

1 answer

2

Good morning, some time ago I had a similar problem, with dates, maybe this function could help you

DateTime.FromOADate(double);
DateTime.FromBinary(long);
DateTime.FromFileTime(long);
DateTime.FromFileTimeUtc(long);

Each of the above methods returns a DateTime object, taking parameter formats of numeric dates, such as the number you gave as an example.

Afterwards if you like it from the database you can give the date format you want, with the following code you return the date in a dormato dd / mm / yyyy

SELECT CAST(Convert(VARCHAR(10), Fecha, 103) AS VARCHAR(10)) FROM [tuTabla] . Make a double conversion, so that it comes out as a string type, and its integrity is not affected.

Another thing that may be affecting is the date code that you use, you can use ISO (112) that returns aaammdd, canonical ODBC (120) yyyy-mm-dd, Japanese (111) yy / mm / dd

    
answered by 27.10.2016 / 16:33
source