First of all I want to say I'm using Firebird and do not know if this is problem with other database servers.
What I want to do is pass part of command string as parameter.
I have code like this:
using (FbConnection con = new FbConnection(M.Baza.connectionString))
{
con.Open();
using (FbCommand cmd = new FbCommand("DELETE FROM STAVKA WHERE VRDOK = @VRDOK AND BRDOK = @BRDOK AND ROBAID IN (@ROBAID)", con))
{
cmd.Parameters.AddWithValue("@VRDOK", vrDok);
cmd.Parameters.AddWithValue("@BRDOK", brDok);
cmd.Parameters.AddWithValue("@ROBAID", robaId); //robaId is string which looks like this '15, 18, 16, 14, 28'
cmd.ExecuteNonQuery();
if (showMessage)
MessageBox.Show("Stavke uspesno obrisane!");
}
}
The code above is not working and it drops error
unable to cast object of type 'system.Int32` to type 'system.iconvertible'
But when I write code like this:
using (FbConnection con = new FbConnection(M.Baza.connectionString))
{
con.Open();
using (FbCommand cmd = new FbCommand("DELETE FROM STAVKA WHERE VRDOK = @VRDOK AND BRDOK = @BRDOK AND ROBAID IN (" + robaId + ")", con))
{
cmd.Parameters.AddWithValue("@VRDOK", vrDok);
cmd.Parameters.AddWithValue("@BRDOK", brDok);
cmd.ExecuteNonQuery();
if (showMessage)
MessageBox.Show("Stavke uspesno obrisane!");
}
}
It works.
Why is this happening?
Let's say I have command like this "SELECT " + columns + " FROM TABLE"
where columns
is type of string which looks like this column1, column2, column3
and it is working. How I can make same code look like this "SELECT @COLUMNS FROM TABLE"
and pass that @COLUMNS
just like I am passing parameters with cmd.Parameters.AddWithValue("@COLUMNS", columns);