Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi ,
i want to below code into edmx or linq same thing using guide me or send snippets, the data should be return in json format ,for that i am using in wcf service
C#
public Employee[] GetAllEmployee()
        {
            List<employee> lstEmp = new List<employee>();
            SqlConnection Conn = new SqlConnection("Data Source=.\\sezpress;Initial Catalog=Test;User ID=sa;Password=bhss");
            Conn.Open();
            SqlCommand Cmd = new SqlCommand("Select * from Employee", Conn);
            SqlDataReader Reader = Cmd.ExecuteReader();
            while (Reader.Read())
            {
                lstEmp.Add(new Employee()
                {
                    EmpNo = Convert.ToInt32(Reader["EmpNo"]),
                    EmpName = Reader["EmpName"].ToString(),
                    DeptNo = Convert.ToInt32(Reader["DeptNo"]),
                    Salary = Convert.ToInt32(Reader["Salary"])
                });
            }
            Reader.Close();

            Conn.Close();

            return lstEmp.ToArray();
        }
Posted
Updated 21-Oct-13 20:14pm
v3
Comments
CodeBlack 22-Oct-13 2:14am    
what you have tried and error you are getting ?

Hi,

I am not sure, if you want to use C# but this is much simple with the DLL, I use ["Newtonsoft.Json.dll"] found at http://json.codeplex.com/[^].

There are two methods available i.e Serialize and Deserialize data-table

C#
public static string Serialize(object value)
       {
           if (value != null)
           {
               Type type = value.GetType();

               JsonSerializer json = new JsonSerializer();

               json.NullValueHandling = NullValueHandling.Ignore;

               json.ObjectCreationHandling = ObjectCreationHandling.Replace;
               json.MissingMemberHandling = MissingMemberHandling.Ignore;
               json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

               //if (type == typeof(DataRow))
               //    json.Converters.Add(new DataRowConverter());
               //else
               if (type == typeof(DataTable))
                   json.Converters.Add(new DataTableConverter());
               else if (type == typeof(DataSet))
                   json.Converters.Add(new DataSetConverter());

               StringWriter sw = new StringWriter();
               JsonTextWriter writer = new JsonTextWriter(sw);
               //if (this.FormatJsonOutput)
               //    writer.Formatting = Formatting.Indented;
               //else
               writer.Formatting = Formatting.None;

               writer.QuoteChar = '"';
               json.Serialize(writer, value);

               string output = sw.ToString();
               writer.Close();
               sw.Close();

               return output;
           }
           else
           {
               return "{no data found}";
           }
       }

       public static object Deserialize(string jsonText, Type valueType)
       {
           JsonSerializer json = new JsonSerializer();

           json.NullValueHandling = NullValueHandling.Ignore;
           json.ObjectCreationHandling = ObjectCreationHandling.Replace;
           json.MissingMemberHandling = MissingMemberHandling.Ignore;
           json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

           StringReader sr = new StringReader(jsonText);
           JsonTextReader reader = new JsonTextReader(sr);
           object result = json.Deserialize(reader, valueType);
           reader.Close();

           return result;
       }


Hope this helps.

Regards,
NBaua
 
Share this answer
 
Comments
Ranjan.D 22-Oct-13 22:38pm    
Not a solution. Please read the question carefully and post answers. OP is looking for edmx or linq solution.
Nhilesh B 22-Oct-13 22:41pm    
Read my answer's first line, I am proposing, not imposing...
Ranjan.D 22-Oct-13 22:46pm    
Sorry you were partially right with Json but not with edmx or linq :)
Have a look into the below mentioned article to learn how to create a EF based solution.

Using the Entity Framework 4.3 in .NET Development[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900