I ran into the same problem, but mine was making object [2] = object [1] as SqlParameters, similar to what was tried.
Just to add to the stream, I have this simple array of SqlParameters objects added from a method like this,
private SqlParameter GetGenericParamsObject(string name, object data) { return new SqlParameter(name, SetSqlDataType(data.GetType().ToString())) { Direction = Input, Value = data }; }
If there is a simple switch for SetSqlDataType (), then SqlDbType.Int is one of the return types for setting it.
Then i ran
private static void ExecuteSqlCommand(DbContext dbContext, string sql, params object[] sqlParameters) { try { if (dbContext.Database.Connection.State == ConnectionState.Closed) dbContext.Database.Connection.Open(); var cmd = dbContext.Database.Connection.CreateCommand(); cmd.CommandText = sql; foreach (var param in sqlParameters) cmd.Parameters.Add(param); cmd.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine(e); throw; } }
This helps with casting the appropriate data type and moves the parameter name, data type and object from the command method to simplify debugging, and then simply adds the entire SqlParameter to cmd.Parameters.
source share