■ DataTable 클래스 : 객체 컬렉션 구하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


Program.cs

 

 

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.SQLite;

 

using Newtonsoft.Json;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.Title = "DataTable 클래스 : 객체 컬렉션 구하기";

 

            string connectionString = "URI=file:test.db";

 

            using(SQLiteConnection connection = new SQLiteConnection(connectionString))

            {

                connection.Open();

 

                SQLiteCommand command = new SQLiteCommand();

 

                command.Connection  = connection;

                command.CommandType = CommandType.Text;

                command.CommandText = "SELECT * FROM Sample";

 

                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);

 

                DataTable sourceTable = new DataTable();

 

                adapter.Fill(sourceTable);

 

                if(sourceTable.Rows.Count > 0)

                {

                    string json = JsonConvert.SerializeObject(sourceTable);

 

                    List<Sample> targetList = (List<Sample>)JsonConvert.DeserializeObject(json, typeof(List<Sample>));

 

                    foreach(Sample sample in targetList)

                    {

                        Console.WriteLine(sample);

                    }

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요