MySQL Query with parameters

2007-06-06
2013-04-09
  • Alain Léglise
    Alain Léglise
    2007-06-06

    Hi.

    I try to create a SQL Query (select * from) including parameters.
    eg : "select * from mysql.user where user = &pUsername"
    I've use MySQLCommand and created one "&pUsername" named Parameter.
    Unfortunately the myRead.Read() line (look forward) do not read any row, while running pgm.
    What should I do ?

    I'm using then .Net 2 MySQLDriverCS release, with Visual Studio 2005.

    Here is my code :

    --
    ........
    myCommand = new MySQLCommand();
    myCommand.Connection = myConnection; // defined upper
    myCommand.CommandText = "select Host, User, Password from mysql.User where User = ?pUsername";

    myPrm.ParameterName = "?pCod";
    myPrm.DbType = System.Data.DbType.String;
    myPrm.Direction = System.Data.ParameterDirection.Input;
                   
    myCommand.Parameters.Add(myPrm);
    myCommand.Prepare();

    try
    {
        myPrm.Value = "root" ;
        MySQLDataReader myRead = myCommand.ExecuteReaderEx();

        Console.WriteLine("-> " + myRead.FieldCount.ToString());
                   
        while (myRead.Read())
        {
            Message += " " + myRead.GetString(0) + ", " // Message is a string defined in the current class.
                     + myRead.GetString(1) + ", "
                     + myRead.GetString(2);
        }
        myRead.Close();
    }
    catch(Exception exc)
    {
        Message = exc.Message;
    }
    .....
    --