private static void initDbCommand(SqliteCommand cmd, SqlBuilderCommand sqlCmd) { cmd.CommandType = CommandType.Text; cmd.CommandText = sqlCmd.Sql; if (sqlCmd.Transaction != null) { cmd.Transaction = sqlCmd.Transaction as SqliteTransaction; } if (sqlCmd.Parameters != null) { foreach (var item in sqlCmd.Parameters) { SqliteParameter p = new SqliteParameter(item.Name); if (item.DbType != null) { p.DbType = item.DbType.Value; } if (item.Size != null) { p.Size = item.Size.Value; } if (item.Value != null) { p.Value = item.Value; } if (item.Direction != null) { p.Direction = item.Direction.Value; } cmd.Parameters.Add(p); } } }
//生成参数 public static SqlMapper.IDynamicParameters CreateDynamicParameters(SqlBuilderCommand cmd) { if (cmd.Parameters == null && cmd.DynamicParameters == null) { return(null); } var result = new DynamicParameters(); if (cmd.Parameters != null) { foreach (var item in cmd.Parameters) { result.Add(item.Name, item.Value, item.DbType, item.Direction, item.Size); } } if (cmd.DynamicParameters != null) { foreach (var item in cmd.DynamicParameters) { result.AddDynamicParams(item); } } return(result); }
public static int ExecuteNonQuery(this SqlBuilderCommand sqlCmd, SqliteConnection conn) { using (var cmd = conn.CreateCommand()) { initDbCommand(cmd, sqlCmd); return(cmd.ExecuteNonQuery()); } }
public static object ExecuteScalar(this SqlBuilderCommand sqlCmd, SqliteConnection conn) { using (var cmd = conn.CreateCommand()) { initDbCommand(cmd, sqlCmd); return(cmd.ExecuteScalar()); } }
public static Task <T> ExecuteScalarAsync <T>(this IDbConnection cnn, SqlBuilderCommand cmd) { if (cnn == null) { throw new ArgumentNullException(nameof(cnn)); } return(cnn.ExecuteScalarAsync <T>(cmd.Sql, DapperHelper.CreateDynamicParameters(cmd), cmd.Transaction, cmd.CommandTimeout, cmd.CommandType)); }
public static int ExecuteNonQuery(this SqlBuilderCommand sqlCmd, IDbConnectionFactory connFactory) { using (var conn = connFactory.CreateDbConnection() as SqliteConnection) { conn.Open(); using (var cmd = conn.CreateCommand()) { initDbCommand(cmd, sqlCmd); return(cmd.ExecuteNonQuery()); } } }
public static T ExecuteReader <T>(this SqlBuilderCommand sqlCmd, SqliteConnection conn, Func <IDataReader, T> act) { using (var cmd = conn.CreateCommand()) { initDbCommand(cmd, sqlCmd); using (var reader = cmd.ExecuteReader()) { if (act != null) { return(act(reader)); } else { return(default(T)); } } } }
public static Task <IEnumerable <T> > QueryAsync <T>(this IDbConnection cnn, SqlBuilderCommand cmd) { return(cnn.QueryAsync <T>(cmd.Sql, DapperHelper.CreateDynamicParameters(cmd), cmd.Transaction, cmd.CommandTimeout, cmd.CommandType)); }
public static IEnumerable <T> Query <T>(this IDbConnection cnn, SqlBuilderCommand cmd, bool buffered = true) { return(cnn.Query <T>(cmd.Sql, DapperHelper.CreateDynamicParameters(cmd), cmd.Transaction, buffered, cmd.CommandTimeout, cmd.CommandType)); }