public void Open() { if (mState != ConnectionState.Closed) throw new InvalidOperationException(); // Parameter help String pHelp = "Valid parameters:\n" + "Data Source=<database file> (required)\n" + "Version=<version of SQLite (2 or 3)> (default: 2)\n" + "New=True|False (default: False)\n" + "Compress=True|False (default: False)\n" + "UTF8Encoding=True|False (default: False)\n" + "UTF16Encoding=True|False (default: False)\n" + //S"Temp Store=File|Memory (default: File)\n" + "Cache Size=<N> (default: 2000)\n" + "Synchronous=Full|Normal|Off (default: Normal)\n" + "DateTimeFormat=ISO8601|Ticks|CurrentCulture (default: ISO8601)\n" + "Compatibility=[old-date-format][,old-binary-format] (default: None)"; // Parameter defaults String pDB = null; bool newFile = false; bool compress = false; String pSync = "Normal"; //String pTempStore = "File"; String pCacheSize = "2000"; bool UTF8Encoding = false; bool version3 = false; bool UTF16Encoding = false; DateTimeFormat DTFormat = DateTimeFormat.ISO8601; bool oldDateTimeFormat = false; bool oldBinaryFormat = false; // Parse connection string String [] parameters = mpConnStr.Split (";".ToCharArray()); if (parameters.Length == 0) throw new SQLiteException (pHelp); for (int i = 0; i < parameters.Length; i++) { String p = parameters[i].Trim(); if( p.Length == 0 ) // ignore empty item continue; int index = p.IndexOf('='); if( index < 0 ) throw new SQLiteException (pHelp); String paramName = p.Substring(0,index).Trim(); String paramValue = p.Substring(index+1).Trim(); if (paramName.Equals ("Data Source")) pDB = paramValue; else if (paramName.Equals ("Version")) version3 = paramValue.Equals ("3"); else if (paramName.Equals ("New")) newFile = paramValue.Equals ("True"); else if (paramName.Equals ("Compress")) compress = paramValue.Equals ("True"); else if (paramName.Equals ("UTF8Encoding")) UTF8Encoding = paramValue.Equals ("True"); else if (paramName.Equals ("UTF16Encoding")) UTF16Encoding = paramValue.Equals ("True"); else if (paramName.Equals ("Synchronous")) pSync = paramValue; else if (paramName.Equals ("Cache Size")) pCacheSize = paramValue; // else if (paramName.Equals ("Temp Store")) // pTempStore = paramValue; else if (paramName.Equals ("DateTimeFormat")) { if( string.Compare(paramValue,"ISO8601",true) == 0 ) DTFormat = DateTimeFormat.ISO8601; else if( string.Compare(paramValue,"Ticks",true) == 0 ) DTFormat = DateTimeFormat.Ticks; else if( string.Compare(paramValue,"CurrentCulture",true) == 0 ) DTFormat = DateTimeFormat.CurrentCulture; } else if (paramName.Equals ("Compatibility")) { string compatibilityValues = paramValue; foreach( string s in compatibilityValues.Split(',') ) { string val = s.Trim(); if( string.Compare(val,"old-date-format",true) == 0 ) oldDateTimeFormat = true; else if( string.Compare(val,"old-binary-format",true) == 0 ) oldBinaryFormat = true; } } else throw new SQLiteException (pHelp); } if (pDB == null) throw new SQLiteException (pHelp); if (File.Exists (pDB)) { if (newFile) { // Try to delete existing file try { File.Delete (pDB); File.Delete (String.Concat (pDB, "-journal")); } catch (IOException) { throw new SQLiteException ("Cannot create new file, the existing file is in use."); } catch (Exception) { } } } else { if (!newFile) { try { pDB = System.IO.Path.GetFullPath(pDB); } catch(Exception ) { } throw new SQLiteException (String.Concat("File '",pDB,"' does not exist. Use ConnectionString parameter New=True to create new file.")); } } // Open connection if( compress ) Util.CompressFile(pDB); // instantiate sqlite if( version3 ) sqlite = new sqlite3(UTF16Encoding,DTFormat,oldDateTimeFormat); else sqlite = new sqlite2(UTF8Encoding,DTFormat,oldDateTimeFormat,oldBinaryFormat); sqlite.open(pDB); mState = ConnectionState.Open; // Do Pragma's String pPragma; pPragma = String.Concat ("pragma synchronous = ", pSync); sqlite.exec (pPragma); pPragma = String.Concat ("pragma cache_size = ", pCacheSize); sqlite.exec (pPragma); // TODO temp_store pragma not working (?!?) // pPragma = String.Concat ("pragma temp_store = ", pTempStore); // sqlite.exec (pPragma); }
public void Close() { if (mState != ConnectionState.Closed) { if (mpTrans != null) mpTrans.Rollback(); Debug.Assert (mpTrans == null); for (int i = 0; i < mpCmds.Count; ++i) ((SQLiteCommand)mpCmds[i]).UnCompile(); mState = ConnectionState.Closed; if( sqlite != null ) { sqlite.close(); sqlite = null; } } }