private IntPtr Prepare() { var stmt = SQLiteNative.Prepare2(_connection.Handle, CommandText); this.BindParameters(stmt); return(stmt); }
public int ExecuteNonQuery() { _stmt = this.Prepare(); var result = SQLiteNative.Step(_stmt); if (result == SQLiteResult.Done) { var rows = SQLiteNative.Changes(_connection.Handle); return(rows); } if (result == SQLiteResult.Error) { throw new SQLiteException(result, SQLiteNative.GetErrmsg(_connection.Handle)); } string message; try { message = SQLiteNative.GetErrmsg(_connection.Handle); } catch { message = result.ToString(); } throw new SQLiteException(result, message); }
public void Dispose() { if (_stmt != IntPtr.Zero) { SQLiteNative.Finalize(_stmt); _stmt = IntPtr.Zero; } }
public void Open() { IntPtr handle; var result = SQLiteNative.Open(this.DatabasePath, out handle, 2 | 4, IntPtr.Zero); if (result != SQLiteResult.OK) { throw new SQLiteException(result, string.Format("Could not open database file: {0} ({1})", this.DatabasePath, result)); } this.Handle = handle; }
private void BindParameters(IntPtr stmt) { foreach (var p in this.Parameters) { var name = p.ParameterName; if (!name.StartsWith("@", StringComparison.OrdinalIgnoreCase)) { name = "@" + name; } var index = SQLiteNative.BindParameterIndex(stmt, name); this.BindParameter(stmt, index, p.Value); } }
public void Close() { if (this.Handle != IntPtr.Zero) { try { var result = SQLiteNative.Close(this.Handle); if (result != SQLiteResult.OK) { throw new SQLiteException(result, SQLiteNative.GetErrmsg(this.Handle)); } } finally { this.Handle = IntPtr.Zero; } } }
private void BindParameter(IntPtr stmt, int index, object value) { if (value == null) { SQLiteNative.BindNull(stmt, index); return; } if (value is Int32) { SQLiteNative.BindInt(stmt, index, (int)value); return; } var stringValue = value as string; if (stringValue != null) { SQLiteNative.BindText(stmt, index, stringValue, -1, NegativePointer); return; } if (value is bool) { SQLiteNative.BindInt(stmt, index, (bool)value ? 1 : 0); return; } if (value is Byte || value is UInt16 || value is SByte || value is Int16) { SQLiteNative.BindInt(stmt, index, Convert.ToInt32(value)); return; } if (value is UInt32 || value is Int64) { SQLiteNative.BindInt64(stmt, index, Convert.ToInt64(value)); return; } if (value is Single || value is Double || value is Decimal) { SQLiteNative.BindDouble(stmt, index, Convert.ToDouble(value)); return; } if (value is DateTime) { var dateTimeValue = (DateTime)value; if (_connection.DateTimeAsTicks) { SQLiteNative.BindDouble(stmt, index, SQLiteParameter.ToJulianDay(dateTimeValue)); } else { SQLiteNative.BindText(stmt, index, dateTimeValue.ToString("yyyy-MM-dd HH:mm:ss"), -1, NegativePointer); } return; } var bytesValue = value as byte[]; if (bytesValue != null) { SQLiteNative.BindBlob(stmt, index, bytesValue, bytesValue.Length, NegativePointer); return; } try { SQLiteNative.BindInt(stmt, index, (int)value); } catch { throw new NotSupportedException("Cannot store type: " + value.GetType()); } }