problem using parameters with mysql and C#

J

john coltrane

I am trying to use parameters with mysql and I am not getting any
results returned.

In the following example the "legs" column is defined as INTEGER.
For a simple test I am using the following snippet:


MySqlConnection conn;
conn = new MySqlConnection(connStr);
conn.Open();
statement = "select * from limbs where legs = @legs";
MySqlCommand command = new MySqlCommand(statement, conn);

MySqlParameter param = new MySqlParameter();
param.ParameterName = "@legs";
param.Value = 2;
param.MySqlDbType = MySqlDbType.Int32;
command.Parameters.Add(param);

MySqlDataReader reader;
reader = command.ExecuteReader();
while ( reader.Read() ) {
Console.WriteLine("{0}|{1}|{2}|", reader[ "thing" ],
reader[ "legs" ], reader[ "arms" ]);
}
reader.Close();
} catch ( MySqlException mysql_e ) {
Console.WriteLine(mysql_e.Message);
}


No results are returned and no error is generated.
I am using MySql 5.1 with MySql Connect Net 1.0.7 and .Net Framework
3.0.

thanks for any help
 
K

Kerry Moorman

john,

I think that the parameter placeholder for mysql would be "?legs", not
"@legs".

You would need to change it in your sql select statement and your parameter
name.

Love your music, by the way.

Kerry Moorman
 
J

john coltrane

john,

I think that the parameter placeholder for mysql would be "?legs", not
"@legs".

You would need to change it in your sql select statement and your parameter
name.

Love your music, by the way.

Kerry Moorman



john coltrane said:
I am trying to use parameters with mysql and I am not getting any
results returned.
In the following example the "legs" column is defined as INTEGER.
For a simple test I am using the following snippet:
        MySqlConnection conn;
        conn = new MySqlConnection(connStr);
        conn.Open();
        statement = "select * from limbs where legs = @legs";
        MySqlCommand command = new MySqlCommand(statement, conn);
        MySqlParameter param = new MySqlParameter();
        param.ParameterName = "@legs";
        param.Value = 2;
        param.MySqlDbType = MySqlDbType.Int32;
        command.Parameters.Add(param);
        MySqlDataReader reader;
        reader = command.ExecuteReader();
        while ( reader.Read() ) {
          Console.WriteLine("{0}|{1}|{2}|", reader[ "thing" ],
reader[ "legs" ], reader[ "arms" ]);
        }
        reader.Close();
      } catch ( MySqlException mysql_e ) {
        Console.WriteLine(mysql_e.Message);
      }
No results are returned and no error is generated.
I am using MySql 5.1 with MySql Connect Net 1.0.7 and .Net Framework
3.0.
thanks for any help- Hide quoted text -

- Show quoted text -

thank you, that works. I was going through the "Pro C# 2008 .Net 3.5
Platform" book by Troelsen and the '@' symbol is used for insert
statements and stored procedures.
Are different prefix symbols used for different types of sql
statements?

thanks again

john
 
K

Kerry Moorman

john,

Parameter placeholder format seems to be determined by the database being
used. SQL Server uses the "@" prefix, Oracle uses ":", mysql uses "?", etc.

On the other hand, OleDb seems to use just a "?", regardless of database.

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top