關於安碩

【服務項目】

 自動化設備控制系統及機構設計
 PC Based 控制系統設計
 PLC 及 HMI 程式設計
 多軸運動控制
 機器視覺檢測系統設計
 資料收集及遠端監控
 設備電控電路設計及控制盤體配線
 機械手臂整合應用
 舊有設備整理及改善
 客製化程式開發
 IPC / 軸控模組 / IO 模組 代理

系統整合外包服務 E-mail: ling.ryan@gmail.com 歡迎來信連絡

2021年5月1日 星期六

[筆記] C# 讀寫 SQLite


class TestRecord
    {

        private string mDbPath = string.Empty;
        private SQLiteConnection dbConnection;
        public TestRecord(string _DbPath)
        {
            mDbPath = _DbPath;
            dbConnection = new SQLiteConnection("data source=" + mDbPath);
        }

        public void Insert()
        {
            dbConnection.Open();

            string strInsertSql = "insert into TestRecord (ID,DataTime,Point1) values (null,@Updatedate,@Value1)";
            SQLiteCommand command = new SQLiteCommand(strInsertSql, dbConnection);
            command.Parameters.Add(new SQLiteParameter("@Updatedate", DbType.DateTime2));
            command.Parameters.Add(new SQLiteParameter("@Value1", DbType.Decimal));
            DbTransaction trans = dbConnection.BeginTransaction();

            for (int a = 0x01; a < 1000000; a++)
            {
                command.Parameters[0].Value = DateTime.Now;
                command.Parameters[1].Value = 11.1;
                command.ExecuteNonQuery();
            }
            trans.Commit();
            dbConnection.Close();
        }

        public void Delete()
        {
            dbConnection.Open();
            //刪除所有資料
            string strInsertSql = "delete from TestRecord";
            SQLiteCommand command = new SQLiteCommand(strInsertSql, dbConnection);
            command.ExecuteNonQuery();

            //歸零 ID
            strInsertSql = "delete from sqlite_sequence where name = 'TestRecord'";
            command = new SQLiteCommand(strInsertSql, dbConnection);
            command.ExecuteNonQuery();

            //壓縮資料庫
            command = new SQLiteCommand("VACUUM", dbConnection);
            command.ExecuteNonQuery();
            dbConnection.Close();

        }

        public void Query()
        {
            dbConnection.Open();
            string strInsertSql = "SELECT * FROM TestRecord";
            SQLiteCommand command = new SQLiteCommand(strInsertSql, dbConnection);
            SQLiteDataReader sr = command.ExecuteReader();
            int a = 0x00;
            while (sr.Read())
            {
                string ss = sr["ID"].ToString();
                string aaaa = sr["DataTime"].ToString();
                string CCC = sr["Point1"].ToString();
                a++;
                
            }
            dbConnection.Close();

        }

        public void LastRecord()
        {
            dbConnection.Open();
            string strInsertSql = "SELECT * FROM `TestRecord` ORDER BY id DESC LIMIT 0 , 1";
            SQLiteCommand command = new SQLiteCommand(strInsertSql, dbConnection);
            SQLiteDataReader sr = command.ExecuteReader();
            int a = 0x00;
            while (sr.Read())
            {
                string ss = sr["ID"].ToString();
                string aaaa = sr["DataTime"].ToString();
                string CCC = sr["Point1"].ToString();
                a++;

            }
            dbConnection.Close();
        }

        public void aa()
        {
            string path = "d:\\CH01.db";
            SQLiteConnection cn = new SQLiteConnection("data source=" + mDbPath);
            cn.Open();
            SQLiteCommand cmd = cn.CreateCommand();
            cmd.CommandText = "PRAGMA table_info('TestRecord')";
            SQLiteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    Console.Write($"{reader[i]},");
                }
                Console.WriteLine();
            }
            reader.Close();
        }
    }

沒有留言:

張貼留言