■ DataTable 클래스 : Select 메소드에서 DateTime 조건문 사용하기

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


TestProject.zip


Program.cs

 

 

using System;

using System.Globalization;

using System.Data;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

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

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

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

 

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

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            Console.WriteLine("BEGIN MAIN FUNCTION");

 

            Console.Title = "DataTable 클래스 : Select 메소드에서 DateTime 조건문 사용하기";

 

            Console.WriteLine("BEGIN GET TABLE");

 

            DateTime currentTime = DateTime.Now;

 

            DataTable table = GetTable(currentTime, 100);

 

            Console.WriteLine("END GET TABLE");

 

            Console.WriteLine("BEGIN SELECT TABLE");

 

            string filterExpression = string.Format("CreateTime > #{0}#", currentTime.ToString(DateTimeFormatInfo.InvariantInfo));

 

            // 아래와 같이 설정할 수도 있다.

            //string filterExpression = string.Format("CreateTime > '{0}'", currentTime.ToString());

 

            DataRow[] rowArray = table.Select(filterExpression, "ID ASC");

 

            Console.WriteLine("END SELECT TABLE");

 

            Console.WriteLine("BEGIN PRINT RESULT");

 

            Console.WriteLine("--------------------------------------------------");

 

            foreach(DataRow row in rowArray)

            {

                Console.WriteLine(row[0] + "|" + row[1] + "|" + row[2]);

            }

 

            Console.WriteLine("END PRINT RESULT : COUNT={0}", rowArray.Length);

 

            Console.WriteLine("--------------------------------------------------");

 

            Console.WriteLine("CURRENT TIME : " + currentTime.ToString());

 

            Console.WriteLine("END MAIN FUNCTION");

        }

 

        #endregion

 

        #region 데이터 테이블 구하기 - GetTable(currentTime, count)

 

        /// <summary>

        /// 데이터 테이블 구하기

        /// </summary>

        /// <param name="currentTime">현재 시간</param>

        /// <param name="count">카운트</param>

        /// <returns>데이터 테이블</returns>

        private static DataTable GetTable(DateTime currentTime, int count)

        {

            int countLength = count.ToString().Length;

 

            DataTable table = new DataTable();

 

            table.Columns.Add("ID"        , typeof(string  ));

            table.Columns.Add("Subject"   , typeof(string  ));

            table.Columns.Add("CreateTIme", typeof(DateTime));

 

            Random random = new Random(DateTime.Now.Millisecond);

 

            for(int i = 1; i <= count; i++)

            {

                table.Rows.Add

                (

                    "ID" + i.ToString("d" + countLength),

                    "Subject" + i.ToString("d" + countLength),

                    currentTime.AddMinutes(random.Next(-50, 50))

                );

            }

 

            table.AcceptChanges();

 

            return table;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요