Ajout de paramètres à IDbCommand

Je suis entrain de créer une petite fonction d'assistance au retour d'un DataTable. Je voudrais travailler à travers tous les fournisseurs qui ADO.Net prend en charge, j'ai donc pensé à faire tout usage IDbCommand ou DbCommand si possible.

J'ai atteint un point d'achoppement avec le code suivant:

    private static DataTable QueryImpl(ref IDbConnection conn, String SqlToExecute, CommandType CommandType, Array Parameters)
    {
        SetupConnection(ref conn);
        //set the capacity to 20 so the first 20 allocations are quicker...
        DataTable dt = new DataTable();
        using (IDbCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = SqlToExecute;
            cmd.CommandType = CommandType;
            if (Parameters != null && Parameters.Length > 0)
            {
                for (Int32 i = 0; i < Parameters.Length; i++)
                {
                    cmd.Parameters.Add(Parameters.GetValue(i));
                }
            }
            dt.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges);
        }
        return dt;
    }

Lorsque ce code est exécuté, je reçois un InvalidCastException qui stipule ce qui suit:

La SqlParameterCollection accepte uniquement les non-null SqlParameter type des objets, pas des objets String.

Le code tombe sur la ligne:

cmd.Parameters.Add(Parameters.GetValue(i));

Des idées?

Toutes les améliorations au code ci-dessus est apprécié.


Solution réelle:

    private static readonly Regex regParameters = new Regex(@"@\w+", RegexOptions.Compiled);
    private static DataTable QueryImpl(ref DbConnection conn, String SqlToExecute, CommandType CommandType, Object[] Parameters)
    {
        SetupConnection(ref conn);
        DataTable dt = new DataTable();
        using (DbCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = SqlToExecute;
            cmd.CommandType = CommandType;
            if (Parameters != null && Parameters.Length > 0)
            {
                MatchCollection cmdParams = regParameters.Matches(cmd.CommandText);
                List<String> param = new List<String>();
                foreach (var el in cmdParams)
                {
                    if (!param.Contains(el.ToString()))
                    {
                        param.Add(el.ToString());
                    }
                }
                Int32 i = 0;
                IDbDataParameter dp;
                foreach (String el in param)
                {
                    dp = cmd.CreateParameter();
                    dp.ParameterName = el;
                    dp.Value = Parameters[i++];
                    cmd.Parameters.Add(dp);
                }
            }
            dt.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges);
        }
        return dt;
    } 

Merci pour les idées et les liens etc. 🙂

  • Ce n'Paramètres.GetValue faire?
  • msdn.microsoft.com/en-us/library/system.array.getvalue.aspx
  • Belle solution réelle. Cependant, la mise en DateTime.Maintenant à champ DateTime dans MS Access/OleDb échoue.
  • J'ai entendu la solution pour éviter DateTime erreur est de mettre en millisecondes à zéro et qui permettra de résoudre l'incompatibilité de type de données d'erreur. Est-ce vrai? (Je me soucie de MS Access et SQL Server pour le moment).