none
Convert Dictionary to MySqlParameter Array RRS feed

  • Question

  • Hello,

    I did the following and it worked:

    MySqlParameter[] myParams = new[]
    {
        new MySqlParameter("@LoginID", "Login ID"),
        new MySqlParameter("@RealName", "Real Name"),
        new MySqlParameter("@Password", "password"),
        new MySqlParameter("@Role", "admin")
    };
    

    I created a Dictionary object <string, string> that contains the Param Value (@LoginID, etc), and the value is the value from the fields on the form.

    Can I convert the dictionary key/pair values to an Array of MySqlParamater like in the above example, or would there be a different / better way to just pass the dictionary to the query params ?

    Wednesday, May 8, 2019 3:02 AM

Answers

  • Check this:

     

    MySqlParameter[] myParams = myDictionary.Select( p => new MySqlParameter( p.Key, p.Value ) ).ToArray( );
    

     

    Instead of dictionary, you can also work with arrays, or List<MySqlParameter>, or mySqlCommand.Parameters directly.

    Wednesday, May 8, 2019 5:02 AM

All replies

  • Check this:

     

    MySqlParameter[] myParams = myDictionary.Select( p => new MySqlParameter( p.Key, p.Value ) ).ToArray( );
    

     

    Instead of dictionary, you can also work with arrays, or List<MySqlParameter>, or mySqlCommand.Parameters directly.

    Wednesday, May 8, 2019 5:02 AM
  • What if the parmterized data is not all string data, then what? What if all the data didn't come from Textbox.Text?

    Most would use a custom type/object with public properties and not a dictionary.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties

    Wednesday, May 8, 2019 5:45 AM