我们正在为多人游戏开发数据库后端。服务器是用 C# 编写的,并通过 Npgsql 与 Postgres 数据库通信。
现在,手册展示了如何使用准备好的语句:
NpgsqlCommand command = new NpgsqlCommand("select * from tablea where column1 = :column1", conn);
// Now add the parameter to the parameter collection of the command specifying its type.
command.Parameters.Add(new NpgsqlParameter("column1", NpgsqlDbType.Integer);
// Now, prepare the statement.
command.Prepare();
// Now, add a value to it and later execute the command as usual.
command.Parameters[0].Value = 4;
它指出准备好的语句仅在数据库会话中有效。我现在有两个问题:
1.) 如果我创建一个具有相同命令文本和参数类型的新 NpgsqlCommand 对象,服务器是否会识别该命令已准备好,或者我是否必须保留该对象并在再次执行之前简单地更改变量?在示例中,命令对象在查询之后被释放。
2.) 尽管我们只有这种风格的简单语句,但准备好的语句可能会提高性能:
SELECT f1,f2 FROM t1
UPDATE t1 SET f1=1, f2=2
有可能连续执行数百个具有相同样式的查询 - 当前为每个查询创建一个新的 NpgsqlCommand(以及 NpgSql 池中的 NpgsqlConnection)对象。
谢谢!