Exemplo n.º 1
0
        public HttpResponseMessage GetWithUri([FromUri] ParamsObject paramsObject)
        {
            JavaScriptSerializer serializer          = new JavaScriptSerializer();
            List <Dictionary <string, object> > rows = new List <Dictionary <string, object> >();

            ///HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "");
            ///
            try
            {
                //List<string> cities = new List<string>();

                var connection = ConfigurationManager.ConnectionStrings["datalister_database"].ConnectionString;
                using (SqlConnection con = new SqlConnection(connection))
                {
                    using (SqlCommand cmd = new SqlCommand("proc_Auto_CompanyName", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@State", SqlDbType.VarChar).Value     = paramsObject.st1.Trim();
                        cmd.Parameters.Add("@ParamName", SqlDbType.VarChar).Value = paramsObject.st2.Trim();
                        con.Open();


                        DataTable      dt = new DataTable();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(dt);
                        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);
                        }


                        //SqlDataReader reader = cmd.ExecuteReader();
                        //if (reader.HasRows)
                        //{
                        //    while (reader.Read())
                        //    {
                        //        //cities.Add(reader["city"].ToString());
                        //        XmlDocument doc = new XmlDocument();
                        //        string sxml = reader[0].ToString();
                        //        doc.LoadXml(sxml);
                        //        string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
                        //        response = Request.CreateResponse(HttpStatusCode.OK, json);
                        //    }
                        //}
                        //else
                        //{
                        //    Console.WriteLine("No rows found.");
                        //}
                        //reader.Close();
                    }
                }
            }
            catch (SystemException ex)
            {
            }
            Records rc = new Records();

            rc.fieldsjsn = rows;
            string sreturn = "";

            try
            {
                serializer.MaxJsonLength = int.MaxValue;
                sreturn = serializer.Serialize(rc);
                JavaScriptSerializer j = new JavaScriptSerializer();
                j.MaxJsonLength = int.MaxValue;
                object a = j.Deserialize(sreturn, typeof(object));
            }
            catch (Exception xx)
            {
            }



            var res = Request.CreateResponse(HttpStatusCode.OK);

            res.Content = new StringContent(sreturn.ToString(), Encoding.UTF8, "text/html");


            return(res); //sreturn.Replace(@"\", "");
        }
Exemplo n.º 2
0
        public HttpResponseMessage GetWithUri([FromUri] ParamsObject paramsObject)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
            ///HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "");
            ///
            try
            {
                //List<string> cities = new List<string>();

                var connection = ConfigurationManager.ConnectionStrings["datalister_database"].ConnectionString;
                using (SqlConnection con = new SqlConnection(connection))
                {
                    using (SqlCommand cmd = new SqlCommand("proc_Auto_CompanyName", con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@State", SqlDbType.VarChar).Value = paramsObject.st1.Trim();
                        cmd.Parameters.Add("@ParamName", SqlDbType.VarChar).Value = paramsObject.st2.Trim();
                        con.Open();

                        DataTable dt = new DataTable();
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        da.Fill(dt);
                        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);
                        }

                        //SqlDataReader reader = cmd.ExecuteReader();
                        //if (reader.HasRows)
                        //{
                        //    while (reader.Read())
                        //    {
                        //        //cities.Add(reader["city"].ToString());
                        //        XmlDocument doc = new XmlDocument();
                        //        string sxml = reader[0].ToString();
                        //        doc.LoadXml(sxml);
                        //        string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
                        //        response = Request.CreateResponse(HttpStatusCode.OK, json);
                        //    }
                        //}
                        //else
                        //{
                        //    Console.WriteLine("No rows found.");
                        //}
                        //reader.Close();
                    }
                }

            }
            catch (SystemException ex)
            {

            }
            Records rc = new Records();
            rc.fieldsjsn = rows;
            string sreturn = "";
            try
            {
                serializer.MaxJsonLength = int.MaxValue;
                sreturn = serializer.Serialize(rc);
                JavaScriptSerializer j = new JavaScriptSerializer();
                j.MaxJsonLength = int.MaxValue;
                object a = j.Deserialize(sreturn, typeof(object));

            }
            catch(Exception xx)
            {

            }

            var res = Request.CreateResponse(HttpStatusCode.OK);
            res.Content = new StringContent(sreturn.ToString(), Encoding.UTF8, "text/html");

            return res; //sreturn.Replace(@"\", "");
        }