Reputation: 15
I am getting an error with an SQL statement:
must declare the table variable @table
I don't know how I should fix it. Here is the code for the part that makes the query (for example 3 is Communications, 4 is Radio:basic, 5 is 45, 6 is 5, and 7 is 0).
string table = "Skills" + info[3];
SqlCommand cmd = new SqlCommand("use palladium; insert into @category(skill, basepercent, lvlpercent, special) values ( @skillname , @base , @lvl , @special);");
cmd.Parameters.AddWithValue("@category", table);
cmd.Parameters.AddWithValue("@skillname", info[4]);
cmd.Parameters.AddWithValue("@base", info[5]);
cmd.Parameters.AddWithValue("@lvl", info[6]);
cmd.Parameters.AddWithValue("@special", info[7]);
general.dbsend(cmd, connection);
Upvotes: 1
Views: 614
Reputation: 16137
You can't have a table name as a variable in a static query. You can in a dynamic query, by formatting the table name in the query rather than supplying it as a parameter.
SqlCommand cmd = new SqlCommand("use palladium; insert into ["+table.Replace("]","]]")+"](skill, basepercent, lvlpercent, special) values ( @skillname , @base , @lvl , @special);");
The replace bit guards against SQL Injection.
Upvotes: 2