2015年5月29日 星期五

C# ListBox 如何新增ListBox裡面的item項目的背景顏色 或字體顏色

         
            ListBox預設是存放字串, 如果ListBox裡面的item要做到背景色或字型顏色的變化,就 要使用Drawitem事件調適,傳統的方法是不合適的。


           //--------產生ListBox
          main
         {
            ListBox SearchListBoxResult = new ListBox();
            SearchListBoxResult.Name = "SearchListBoxResult_ListBox";
            SearchListBoxResult.Tag = "AriesItemTitle";
            SearchListBoxResult.Left = 5;
            SearchListBoxResult.Top = 65;
            SearchListBoxResult.Width = 200;
            SearchListBoxResult.Height = 300;
            SearchListBoxResult.Items.Add(new MyListBoxItem(Color.White , "123" ));

            SearchListBoxResult.DoubleClick += SearchListBoxResult_Click;
            SearchListBoxResult.DrawItem += SearchListBoxResult_DrawItem;
            this.tabPage3.Controls.Add(SearchListBoxResult);
         
            SearchListBoxResult.DrawMode = DrawMode.OwnerDrawFixed;//----啟動drawitem事件

        }

        /// <summary> 製作一個ListBox item項目顏色的增加
        /// 製作一個ListBox item項目顏色的增加
        /// </summary>
        public class MyListBoxItem
        {
            public MyListBoxItem(Color c, string m)
            {
                ItemColor = c;//------顏色
                Message = m;//------- string 文字
            }
            public Color ItemColor { get; set; }
            public string Message { get; set; }
        }

        /// <summary>
        /// ListBox繪畫底圖的事件功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchListBoxResult_DrawItem(object sender, DrawItemEventArgs e)
        {
            ListBox Temp = new ListBox();
            for (int i = tabPage3.Controls.Count - 1; i >= 0; i--)
            {
                if (this.tabPage3.Controls[i].Name == "SearchListBoxResult_ListBox")
                    Temp = (ListBox)this.tabPage3.Controls[i];
            }
           
            MyListBoxItem item = Temp.Items[e.Index] as MyListBoxItem; // Get the current item and cast it to MyListBoxItem
            if (item != null)
            {
                e.Graphics.FillRectangle(new SolidBrush(item.ItemColor), e.Bounds);
                e.Graphics.DrawString( // mDraw the appropriate text in the ListBox
                    item.Message, // The message linked to the item
                    Temp.Font, // Take the font from the listbox
                    new SolidBrush(Color.Black), // Set the color
                    0, // X pixel coordinate
                    e.Index * Temp.ItemHeight // Y pixel coordinate.  Multiply the index by the ItemHeight defined in the listbox.
                   
                );
             
            }
            else
            {
                // The item isn't a MyListBoxItem, do something about it
            }
        }

2015年5月6日 星期三

SQLite 刪除資料後,檔案大小不會變小的解決方法 Vacuum 指令,也等於壓縮Sqlite資料

       當SQLite資料內容頻繁的做插入、刪除、此時SQLITE檔案並不會變小,這時就可以透過下面的C#語法將資料壓縮。
       筆者進行360W筆資料的移除後發現檔案大小依舊為300MB,當執行vacuum後壓縮資料庫為16MB。同時效能也可以提高。



//--插入進資料庫 這裡的資料一定為唯一的
            SQLiteConnection sqlite_conn;//----連結建立
            SQLiteCommand sqlite_cmd;//--------連結指令建立

            sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
            sqlite_conn.Open();//--------------打開資料庫
            sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令
            sqlite_cmd.CommandText = "vacuum; ";
            sqlite_cmd.ExecuteNonQuery();//----執行

2015年5月5日 星期二

C# SQLite 在寫入資料時如何加快的方法 - 批次處理SQLite 寫入的指令 DbTransaction

SQLite 大家最知道的缺點就是寫入效率太差,但有一個批次處裡的功能可以稍微加快這方面
            效率的問題。
            筆者在處理18000筆資料寫入上傳統做法要10分左右 但使用該方法只需要2秒鐘左右



//-以下為批次處理SQLite語法
            sqlite_conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["sqlite"].ToString());//取得sqlite位置
            sqlite_conn.Open();//--------------打開資料庫
            sqlite_cmd = sqlite_conn.CreateCommand();//--啟動資料庫指令

           List<string> TempResult = new List<string>;
           trans.add("1");//---加入兩筆資料
           trans.add("2");
         

            //--批次交易處理方法
            DbTransaction trans = sqlite_conn.BeginTransaction();

            try
            {
                for (int i = 0; i < TempResult.Count; i++)//---裡面有兩筆資料
                {
                    sqlite_cmd.CommandText = "INSERT INTO QryStock (SQL) VALUES (" + TempResult[i] +")";
                    sqlite_cmd.ExecuteNonQuery();//----執行
                }


                trans.Commit(); // <-------------------批次執行上述的SQL指令

            }
            catch (Exception ex)
            {
                trans.Rollback(); // <-------------------如果有發生錯誤會中斷
                throw; // <-------------------

            }
           
           
           
            sqlite_conn.Close();