Reputation: 165
I'm currently creating a small C# program that inserts data from files into a postgres table.
The code that does the insertion looks like this:
NpgsqlCommand cmd = new NpgsqlCommand(@"INSERT INTO :table(text) VALUES (:word);", con);
cmd.Parameters.AddWithValue("table", table);
cmd.Parameters.AddWithValue("word", line);
cmd.ExecuteNonQuery();
But every time it tries to execute the "ExecuteNonquery" line I get the following error:
An unhandled exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll
Additional information: ERROR: 42601: syntax error at or near "("
I can connect to the database I have checked. The variables table and line also have the correct values at runtime. I just can't figure out what the problem is..
Any suggestions ?
Upvotes: 0
Views: 1832
Reputation: 12476
As far as I know the table can't be a parameter.
What you can do however is use string concatting/formatting for that:
string table = "table";
NpgsqlCommand cmd = new NpgsqlCommand(string.Format(@"INSERT INTO {0}(text) VALUES (:word);", table), con);
Guess that would work (didn't test it).
Upvotes: 4