記錄一次數據庫操做的問題,數據庫文件佔用,動態庫佔用的能夠瞅瞅

/// <summary>
        /// 根據數據ID獲取數據信息
        /// </summary>
        /// <param name="dataId">數據ID</param>
        /// <returns>返回實體對象</returns>
        public DataInfo GetDataById(string dataId)
        {
            DataInfo temData = null ;

            if (string.IsNullOrEmpty(dataId))
            {
                return temData;
            }

            //DB 狀態判斷
            if (dbConnection.State != ConnectionState.Open)
            {
                return temData;
            }

            //拼接sql
            string sqlStr = string.Format("select "
                                          +"DataId,"
                                          +"FromTime,"
                                          +"ToTime,"
                                          +"BlockTime,"
                                          +"BlockCount,"
                                          +"ValidBlockCount,"
                                          +"DataFileName,"
                                          +"IndexFileName,"
                                          +"LastAccessDate,"
                                          +"Status "
                                          +"from {0} "
                                          +"where DataID='{1}';", tableName, dataId);

            SQLiteCommand cmd = null;
            SQLiteDataReader reader = null;

            try
            {
                cmd = new SQLiteCommand(sqlStr, dbConnection);
                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    temData = new DataInfo();
                    temData.FromTime = DateTime.Parse(reader["FromTime"].ToString());
                    temData.DataId = reader["DataId"].ToString();
                    temData.EndTime = DateTime.Parse(reader["ToTime"].ToString());
                    temData.BlockTime = int.Parse(reader["BlockTime"].ToString());
                    temData.BlockCount = int.Parse(reader["BlockCount"].ToString());
                    temData.DownloadedBlockCount = int.Parse(reader["ValidBlockCount"].ToString());
                    temData.DataFileName = reader["DataFileName"].ToString();
                    temData.IndexFileName = reader["IndexFileName"].ToString();
                    temData.LastAccessTime = DateTime.Parse(reader["LastAccessDate"].ToString());

                    temData.Status = int.Parse(reader["Status"].ToString()) == 0 ?
                                    TDataStatus.EDownloading : int.Parse(reader["Status"].ToString()) == 1 ?
                                    TDataStatus.EDownloaded : TDataStatus.EDownloadFaild;
                }
            }
            catch (Exception e)
            {
                throw new Exception("查詢信息失敗" + e.ToString());
            }
            finally {
                if (cmd != null)
                {
                    cmd.Dispose();
                }
                if (reader != null) 
                {
                    reader.Close();
                    reader.Dispose();
                }
            }

            return temData;
        }

複製代碼

一個完整的代碼先甩上來,記錄兩個重點:sql

  • 建立鏈接

    建立的時候對象聲明出來,不要直接去newbash

  • 關閉鏈接

    關閉的時候注意看下函數

    複製代碼
    cmd = SQLiteCommand(sqlStr, dbConnection);
      reader = cmd.ExecuteReader();
    複製代碼
    複製代碼
    這兩行代碼徹底釋放很重要,否則各類問題,雖然都是臨時變量,函數執行完就會回收,可是系統會回收cmd,當回收cmd的時候,因爲cmd建立的時候用的dbConnection,而dbConnection此時並未釋放,那麼致使的結果是cmd也沒法釋放。 第二點,析構順序問題不大吧,必然先釋放reader,reader釋放的時候cmd被佔用,因此先把cmd先回收了,再次回收reader, finally裏邊的這兩個都必須回收,否則就會出問題的
相關文章
相關標籤/搜索