SQLite Reader

SQLiteツール
SQLite
Table of Content
using System;
using System.Data.SQLite;

namespace PassFlag
{
    class Program
    {
        // unix epochをDateTimeで表した定数
        static DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);


        static void Main(string[] args)
        {
            Boolean blFlag = false;

            // 時刻をUTCに変換
            DateTime dateTime = new DateTime(2020, 01, 09, 10, 00, 10, DateTimeKind.Local);
            dateTime = dateTime.ToUniversalTime();

            // unix epochからの経過秒数を求める
            long unixTime = (long)dateTime.Subtract(UnixEpoch).TotalSeconds;

            //blFlag = CheckPassFlag("1578531610");
            blFlag = CheckPassFlag(unixTime.ToString());
        }

        /// <summary>
        /// CheckPassFlag
        /// </summary>
        /// <param name="strUTC"></param>
        /// <returns></returns>
        static Boolean CheckPassFlag(string strUTC)
        {
            string result = "0";

            long nStartTime = Convert.ToInt64(strUTC);
            long nFinishTime = nStartTime + 20;

            using (SQLiteConnection conn = new SQLiteConnection("Data Source=" + @"E:\L_LOG.DB"))
            {
                conn.Open();

                SQLiteCommand cmd = conn.CreateCommand();

                cmd.CommandText = "SELECT COUNT(*) AS CNT " +
                                    "FROM L_LOG " +
                                    "WHERE Msg like 'トリガが発生%'  " +
                                    "AND " +
                                    "L_Type = 8 " +
                                    "AND  " +
                                    "LOGDT >= " + nStartTime + " " +
                                    "AND " +
                                    "LOGDT <= " + nFinishTime;

                using (SQLiteDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result = reader["CNT"].ToString();
                    }
                }
                conn.Close();
            }

            if ("0".Equals(result))
            {
                return false;
            }

            return true;
        }

        // DateTimeをUNIX時間に変換するメソッド
        public static long ToUnixTime(DateTime dateTime)
        {
            // 時刻をUTCに変換
            dateTime = dateTime.ToUniversalTime();

            // unix epochからの経過秒数を求める
            return (long)dateTime.Subtract(UnixEpoch).TotalSeconds;
        }
    }
}

コメント

タイトルとURLをコピーしました