1
我想为一个查询作为命令参数提供的表名,就像这样:如何在Npgsql中提供查询的表名作为命令参数?
public class Foo
{
private const String myTableName = "mytable";
public void Bar()
{
NpgsqlCommand command = new NpgsqlCommand("SELECT * from :tableName", connection);
command.Parameters.Add(new NpgsqlParameter("tableName", DbType.String));
command.Parameters[0].Value = myTableName;
}
}
这似乎导致此查询:"SELECT * from E'mytable'"
导致错误(介意单引号)。
我真的需要为此做字符串连接吗?它不会从安全角度看不要紧,因为表的名称不能被用户,但字符串连接为始终创建SQL查询的改变让我毛骨悚然......
感谢, 埃里克
表名总是常量。谢谢。 – EricSchaefer 2009-07-30 11:19:35