I am trying to modify a table within a database, as follows:
using (SqlCeCommand com = new SqlCeCommand("INSERT INTO RamResults(Result, Date) VALUES(@num, @date)", con))
{
com.Parameters.AddWithValue("@num", num);
com.Parameters.AddWithValue("@hostname2", hostname2);
com.Parameters.AddWithValue("@date", Form1.date);
com.ExecuteNonQuery();
}
Although the table name has a variable prefix (hostname2). So I have tried to do the following:
("INSERT INTO @hostname2 + RamResults(Result, Date) VALUES(@num, @date)", con))
But no luck, anyone have any ideas to solve this?
Error Message:
There was an error parsing the query. [ Token line number = 1,Token
line offset = 13,Token in error = @hostname2 ]
If you use prepared statements like this,
@hostname2will be replaced to'value'.You should use
string.Format()instead:BUT only use string.Format for your hostprefix, NOT for user input! This way you can prevent SQL injections.