Help with use of .NET Generics/Dictionary in replacing my Arrays

Posted by Rollo R on Stack Overflow See other posts from Stack Overflow or by Rollo R
Published on 2011-03-08T07:58:00Z Indexed on 2011/03/08 8:10 UTC
Read the original article Hit count: 241

Filed under:
|
|
|

Hello, I have these code:

Mypage.cs

string[] strParameterName = new string[2] {"?FirstName", "?LastName"};
    string[] strParameterValue = new string[2] {"Josef", "Stalin"};        
    MyConnection.MySqlLink(strConnection, strCommand, strParameterName, strParameterValue, dtTable);

Myclass.cs

public static void MySqlLink(string strConnection, string strCommand, string[] strParameterName, string[] strParameterValue, DataTable dtTable)
{
    dtTable.Clear();
    MySqlConnection MyConnection = new MySqlConnection(strConnection);
    MySqlCommand MyCommand = new MySqlCommand(strCommand, MyConnection);

    for (int i = 0; i < strParameterName.Length; i++)
    {
        MyCommand.Parameters.AddWithValue(strParameterName[i].ToString(), strParameterValue[i].ToString());
    }

    MySqlDataAdapter MyDataAdapter = new MySqlDataAdapter(MyCommand);
    MyDataAdapter.Fill(dtTable);
}

And then my Sql Command will be something like "SELECT * FROM MyTable WHERE FirstName=?FirstName AND LastName=?LastName"

As you can see, I am using arrays for the Parameter Name and Parameter Value and they both have to "match" with each other and ofcourse the Sql Command.

Someone recommended to me that I use .NET "Dictionary" instead of arrays. Now I have never used that before. Can someone show me a relative example of how I am to use .NET Dictionary here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET