No Stored procedures in MySQL 4 how to pass variables to update command

  • Thread starter Thread starter Gert
  • Start date Start date
G

Gert

Hi,

I need to put information from variables (not only strings) to a mysql
database using update. But no I found out that stored procedures are not
there in mysql 4.

Please help,
Gert
 
Hi,

1. Build query string from variables i.e:

string strStringParameter = "example";
int nIntegerParameter = 10;

string strQuery = "UPDATE TableName SET " +
"Column1 = '" + SafeFromInjection(strStringParameter) + "', "
"'Column2 = " + nIntegerParameter.ToString() + " WHERE Column1 = 1;"

2. (Preffered) Use Parameters

string strStringParameter = "example";
int nIntegerParameter = 10;

string strQuery = "UPDATE TableName SET " +
"Column1 = @StringParameter, "
"Column2 = @IntegerParameter;"

MySqlCommand oCommand = new MySqlCommand(strQuery, oConnection);
MySqlParameter oParameter;

oCommand.CommandText = strQuery;

oParameter = new MySqlParameter("@StringParameter", MySqlType.VarChar);
oParameter.Value = strStringParameter;
oCommand.Parameters.Add(oParameter);

oParameter = new MySqlParameter("@IntegerParameter", MySqlType.Int);
oParameter.Value = nIntegerParameter;
oCommand.Parameters.Add(oParameter);

oCommand.ExecuteNonQuery();

sorry, i wrote it without checking and compiling. note 2nd example is for
managed mysql provider, you can use it in the same way for OLEDB.

Hope this helps
 
Sorry i forgot to describe what SafeFromInjectio function is.
It should at least replace ' character with ''.
 
Hi,

Thanks. Now the array of bytes can be stored with the update command. But
how to get it back.
I found a sample to use the reader.getsqlbinary(0).value but this is not
there for the mysql connector (1.05).

Please help

Best regards,
Gert
 
Hi,

Use MySqldataReader.GetBytes(...)

For more info check your MySqlDirect/MySqlFX documentation.
 
Elo,

One more thing - if this column is BLOB you can use GetMySqlBlob() method
instead.

hope this helps
 
Back
Top