如何使用edmx或linq to sql类将整个表数据转换为数组 [英] How can i fectch whole table data into array using edmx or linq to sql classes

查看:113
本文介绍了如何使用edmx或linq to sql类将整个表数据转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i想要使用指南或发送片段将代码转换为edmx或linq相同的东西,数据应该以json格式返回,因为我在wcf服务中使用

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

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();
        }

推荐答案





我不是当然,如果你想使用C#,但这对于DLL来说非常简单,我使用在 http:/中找到的[Newtonsoft.Json.dll] /json.codeplex.com/ [ ^ ]。



有两种方法可用,即序列化和反序列化数据表



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

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;
       }





希望这会有所帮助。



问候,

NBaua



Hope this helps.

Regards,
NBaua


查看下面提到的文章,了解如何创建基于EF的解决方案。



使用实体框架4.3在.NET开发中 [ ^ ]
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[^]


这篇关于如何使用edmx或linq to sql类将整个表数据转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆