/// <summary> /// Opens up a new connection with a given file. /// At the file's location the connector also places the necessary library files. /// </summary> /// <param name="filename">The file which is storing the DB.</param> /// <exception cref="Exception">Thrown if the connection is already open or an internal /// library error occured while executing sqlite3_open().</exception> /// <exception cref="ArgumentNullException">Thrown if the provided file name is /// null or empty.</exception> public void open(string filename) { if (this.IsOpen) { throw new Exception("DB connection is already open."); } this.filename = filename; if (String.IsNullOrEmpty(filename)) { throw new ArgumentNullException("filename", "The parameter is null or empty."); } try { SQLitePInvoke.installUnmanagedLib(Path.GetDirectoryName(filename)); if (SQLitePInvoke.sqlite3_open(this.filename, ref db) != SQLitePInvoke.SQLITE_OK) { throw new Exception("Error executing sqlite3_open()!"); } this.IsOpen = true; } catch (Exception e) { this.db = IntPtr.Zero; throw new Exception("Could not connect to DB.", e); } }
/// <summary> /// Releases the unmanaged resources use by the SQLite3 and /// optionally releases the managed resources. /// </summary> /// <param name="disposing">\c true to release both managed and /// unmanaged resources; \c false to release only managed resources. /// </param> protected virtual void Dispose(bool disposing) { if (disposing) // release (un)managed resources { if (this.db != IntPtr.Zero) { SQLitePInvoke.sqlite3_close(this.db); this.db = IntPtr.Zero; this.IsOpen = false; } } }
/// <summary> /// Executes an SQL statemant on an opened connection. Before using /// execute ensure that you have opened up the connection using the /// open method. This method shell be used for data manipulation /// statemants. If you'd like to query data use query(). /// </summary> /// <param name="sql">The SQL statemant to execute.</param> public void execute(string sql) { try { IntPtr errMsgPtr = IntPtr.Zero; if (SQLitePInvoke.sqlite3_exec(db, sql, ref errMsgPtr) != SQLitePInvoke.SQLITE_OK) { var errMsg = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(errMsgPtr); SQLitePInvoke.sqlite3_free(errMsgPtr); throw new Exception( String.Format("Error executing sqlite3_exec() - {0}!", errMsg)); } } catch (Exception e) { throw new Exception("Could not execute SQL statement.", e); } }
/// <summary> /// Execute a data query statemant (\c SELECT). This method differs from /// execute() only in that it fills the LastQuery property. You may use /// this method for data manipulation statemants as well but this would /// clear the LastQuery property. /// </summary> /// <param name="sql">The sql statemant to query the database.</param> public void query(string sql) { LastQuery = new List <Dictionary <string, string> >(); try { IntPtr errMsgPtr = IntPtr.Zero; if (SQLitePInvoke.sqlite3_exec(db, sql, ref errMsgPtr, query) != SQLitePInvoke.SQLITE_OK) { var errMsg = Marshal.PtrToStringAnsi(errMsgPtr); SQLitePInvoke.sqlite3_free(errMsgPtr); throw new Exception( String.Format("Error executing sqlite3_exec() - {0}!", errMsg)); } } catch (Exception e) { throw new Exception("Could not execute SQL statement.", e); } }