/// <summary> /// Sets a parameter for a command execution. /// </summary> /// <param name="member">ORM member that contains the parameter name (should be the same as database field name).</param> /// <param name="value">Parameter value.</param> public static void SetParameter(ORMEntityMember member, object value) { SQLiteParameter sqlParam; try { if (value is Image) { Image img = value as Image; sqlParam = new SQLiteParameter(member.Attribute.FieldName, DbType.Binary); sqlParam.Value = (byte[])BinaryUtils.ImageToByteArray(img); } else if (value is byte[]) { sqlParam = new SQLiteParameter(member.Attribute.FieldName, DbType.Binary, ((byte[])value).Length); sqlParam.Value = value; } else if (ReflectionUtils.IsSubclassOfRawGeneric(typeof(ORMEntity <>), value.GetType())) { // Get primary key value of the ORMEntity instance sqlParam = new SQLiteParameter(member.Attribute.FieldName, ORMSqliteDriver.GetPrimaryKeyValue(value)); } else { sqlParam = new SQLiteParameter(member.Attribute.FieldName, value); } ORMSqliteDriver.parameters.Add(sqlParam); } catch (Exception ex) { Logger.LogError(ex); throw ex; } }
public static byte[] ExecuteBlob(string sql) { using (SQLiteCommand cmd = new SQLiteCommand(sql, ORMSqliteDriver.Connection)) { ORMSqliteDriver.SetCommandParameters(cmd); return((byte[])cmd.ExecuteScalar()); } }
/// <summary> /// Execute a SQL sentence for INSERT, UPDATE or DELETE. /// </summary> /// <param name="sql">SQL sentence to execute.</param> /// <returns>An integer with the number of affected rows.</returns> public static int ExecuteNonQuery(string sql) { using (SQLiteCommand cmd = new SQLiteCommand(sql, ORMSqliteDriver.Connection)) { ORMSqliteDriver.SetCommandParameters(cmd); return(cmd.ExecuteNonQuery()); } }
/// <summary> /// Execute a query command. /// </summary> /// <param name="sql">SQL sentence to execute.</param> /// <returns>A <see cref="DbDataReader"/> instance for data accessing.</returns> public static DbDataReader ExecuteReader(string sql) { using (SQLiteCommand cmd = new SQLiteCommand(sql, ORMSqliteDriver.Connection)) { ORMSqliteDriver.SetCommandParameters(cmd); return(cmd.ExecuteReader()); } }
/// <summary> /// Execute a query and return the values in a <see cref="DataTable"/> instance. /// </summary> /// <param name="sql">SQL sentence.</param> /// <returns>A <see cref="DataTable"/> instance filled with the query results.</returns> public static DataTable ExecuteDataTable(string sql) { using (SQLiteCommand cmd = new SQLiteCommand(sql, ORMSqliteDriver.Connection)) { ORMSqliteDriver.SetCommandParameters(cmd); using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd)) { System.Data.DataTable dt = new System.Data.DataTable(); da.Fill(dt); return(dt); } } }
/// <summary> /// Execute a query command that returns a single <see cref="String"/> value. /// </summary> /// <param name="sql">SQL sentence to execute.</param> /// <param name="defaultValue">Returning value when the query doesn't result any value.</param> /// <returns>The resulting <see cref="String"/> value.</returns> public static string ExecuteString(string sql, string defaultValue) { using (SQLiteCommand cmd = new SQLiteCommand(sql, ORMSqliteDriver.Connection)) { ORMSqliteDriver.SetCommandParameters(cmd); object res = cmd.ExecuteScalar(); if (res == null) { return(defaultValue); } else { return(res as string); } } }
/// <summary> /// Execute a query command that returns a single <see cref="String"/> value. /// </summary> /// <param name="sql">SQL sentence to execute.</param> /// <returns>The resulting <see cref="String"/> value.</returns> public static string ExecuteString(string sql) { return(ORMSqliteDriver.ExecuteString(sql, string.Empty)); }