none
DataSet / DataTable Object dönüşümü

    Question

  • Merhaba,

    tam olarak Entity Framework ü kullanmak istemediğim için SQL cümlelerini kendim yazmak istediğim için ADO.NET nesnelerini kullanıyorum. Aşağıdaki gibi Musteri isimli bir sınıf oluşturuyorum ve DataReader nesnesini okuyarak objeye dönüştürüyorum.

    Bu yöntem hakkındaki yorumlarınızı almak istiyorum. Bu şekilde DataSet / DataReader verilerini nasıl bir nesneye dönüştürebilirim.

    Musteri musteri = new Musteri();
    using (SqlDataReader dr = myLib.SorguCalistir("SELECT Id, AdSoyad, SehirId FROM Musteri WHERE MusteriId=129"))
    {
    
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                musteri.MusteriId = (int)dr["Id"];
                musteri.AdSoyad = dr["AdSoyad"].ToString();
                musteri.SehirId = (byte)dr["SehirId"];
                musteri.MeslekId = (byte)dr["MeslekId"];
            }
    
            dr.Close();
        }
    }

    Friday, April 28, 2017 7:34 PM

Answers

  • Hello,

    This is an English speaking forum, we don't understand your language which I'm guessing is Turkish.

    A guess at the translation

    I am using ADO.NET objects because I want to write SQL cvs myself because I do not want to use Entity Framework exactly. I am creating a class named MUST as below and I convert the DataReader object to an object by reading it.

    I want to get your comments on this method. How can I convert an DataSet / DataReader data into an object in this way.

    So let's look at two possibilities, one to collect data into a list, the other into a comma separated string.

    Class 

    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Text;
    public class Operations
    {
        private string ConnectionString = 
            "Data Source=KARENS-PC;Initial Catalog=MasterDetailSimple;Integrated Security=True";
    
        public List<Item> ReadToList()
        {
            var items = new List<Item>();
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT id,FirstName,LastName FROM Customer", cn))
                {
                    cn.Open();
                    var reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            items.Add(new Item()
                            {
                                id = reader.GetInt32(0),
                                FirstName = reader.GetString(1),
                                LastName = reader.GetString(2)
                            });
                        }
                    }
                }
            }
    
            return items;
        }
        public string ReadToString()
        {
            StringBuilder results = new StringBuilder();
    
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT id,FirstName,LastName FROM Customer", cn))
                {
                    cn.Open();
                    var reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            results.AppendLine($"{reader.GetInt32(0)},{reader.GetString(1)},{reader.GetString(2)}");
                        }
                    }
                }
            }
    
            return results.ToString();
        }
    }
    public class Item
    {
        public int id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    

    Form code, in this case a button click event

    var ops = new Operations();
    var results = ops.ReadToList();
    foreach (var item in results)
    {
        Console.WriteLine($"{item.id}, {item.FirstName}, {item.LastName}");
    }
    Console.WriteLine();
    var results1 = ops.ReadToString();
    Console.WriteLine(results1);

    Data from SQL-Server Management Studio

    Results from code above

    1, Stefanie, Buckley
    2, Jane, Mc Gee
    3, Lee, Warren
    4, Regina, Forbes
    5, Dennis, Nunez
    6, Myra, Zuniga
    7, Annie, Larson
    8, Herman, Anderson
    9, Karen, Payne
    11, Ann, Payne
    
    1,Stefanie,Buckley
    2,Jane,Mc Gee
    3,Lee,Warren
    4,Regina,Forbes
    5,Dennis,Nunez
    6,Myra,Zuniga
    7,Annie,Larson
    8,Herman,Anderson
    9,Karen,Payne
    11,Ann,Payne
    
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Mert Köksal Tuesday, May 2, 2017 7:26 PM
    Saturday, April 29, 2017 12:28 AM
    Moderator

All replies

  • English.....
    Friday, April 28, 2017 8:20 PM
  • Hello,

    This is an English speaking forum, we don't understand your language which I'm guessing is Turkish.

    A guess at the translation

    I am using ADO.NET objects because I want to write SQL cvs myself because I do not want to use Entity Framework exactly. I am creating a class named MUST as below and I convert the DataReader object to an object by reading it.

    I want to get your comments on this method. How can I convert an DataSet / DataReader data into an object in this way.

    So let's look at two possibilities, one to collect data into a list, the other into a comma separated string.

    Class 

    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Text;
    public class Operations
    {
        private string ConnectionString = 
            "Data Source=KARENS-PC;Initial Catalog=MasterDetailSimple;Integrated Security=True";
    
        public List<Item> ReadToList()
        {
            var items = new List<Item>();
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT id,FirstName,LastName FROM Customer", cn))
                {
                    cn.Open();
                    var reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            items.Add(new Item()
                            {
                                id = reader.GetInt32(0),
                                FirstName = reader.GetString(1),
                                LastName = reader.GetString(2)
                            });
                        }
                    }
                }
            }
    
            return items;
        }
        public string ReadToString()
        {
            StringBuilder results = new StringBuilder();
    
            using (SqlConnection cn = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand("SELECT id,FirstName,LastName FROM Customer", cn))
                {
                    cn.Open();
                    var reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            results.AppendLine($"{reader.GetInt32(0)},{reader.GetString(1)},{reader.GetString(2)}");
                        }
                    }
                }
            }
    
            return results.ToString();
        }
    }
    public class Item
    {
        public int id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
    
    

    Form code, in this case a button click event

    var ops = new Operations();
    var results = ops.ReadToList();
    foreach (var item in results)
    {
        Console.WriteLine($"{item.id}, {item.FirstName}, {item.LastName}");
    }
    Console.WriteLine();
    var results1 = ops.ReadToString();
    Console.WriteLine(results1);

    Data from SQL-Server Management Studio

    Results from code above

    1, Stefanie, Buckley
    2, Jane, Mc Gee
    3, Lee, Warren
    4, Regina, Forbes
    5, Dennis, Nunez
    6, Myra, Zuniga
    7, Annie, Larson
    8, Herman, Anderson
    9, Karen, Payne
    11, Ann, Payne
    
    1,Stefanie,Buckley
    2,Jane,Mc Gee
    3,Lee,Warren
    4,Regina,Forbes
    5,Dennis,Nunez
    6,Myra,Zuniga
    7,Annie,Larson
    8,Herman,Anderson
    9,Karen,Payne
    11,Ann,Payne
    
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Mert Köksal Tuesday, May 2, 2017 7:26 PM
    Saturday, April 29, 2017 12:28 AM
    Moderator
  • Thank you for the sample code. Thanks for sharing your knowledge.

    Tuesday, May 2, 2017 7:28 PM