使用参数的 npgsql 语句 [英] statement for npgsql using parameter

查看:76
本文介绍了使用参数的 npgsql 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用驱动程序 npgsql 在 sql 查询中传递参数:

I pass the parameters in the sql query using the driver npgsql:

SqlCommand = new NpgsqlCommand();
....
SqlCommand.CommandText = "SELECT id,name FROM table1 WHERE field1=:param2 ORDER BY name;";
SqlCommand.Parameters.AddWithValue("param2", 1);

此查询正确执行并发出了必要的数据,但只要我在选择"部分中向 sql 添加参数

This query executed correctly and issued the necessary data, but as soon as I add parameter to the sql in the section "select"

SqlCommand.CommandText = "SELECT id,name :param1 FROM table1 WHERE field1=:param2 ORDER BY name;";
SqlCommand.Parameters.AddWithValue("param1", ",field1");
SqlCommand.Parameters.AddWithValue("param2", 1);

它给了我一些废话.理论上这个对服务器的请求被视为

it gives me some kind of nonsense. In theory this request to the server is to be treated as

SELECT id,name,field1 FROM table1 WHERE field1=1 ORDER BY name;

但它没有发生.这就提出了一个问题:有没有办法使用此类参数动态插入字段列表?

but it did not happen. This raises the question: is there a way to dynamically insert a list of fields using suchlike parameters?

推荐答案

不幸的是,Npgsql 不支持您尝试执行的操作.NpgsqlParameter 值应该仅用作 where 子句中的参数值.为了按照您的意图动态添加字段名称,您必须使用字符串连接手动创建查询.

Unfortunately, Npgsql doesn't have support for what you are trying to do. NpgsqlParameter values are supposed to be only used as parameter values in the where clause. In order to add field names dynamically as you intend, you will have to create the query manually by using string concatenation.

希望能帮到你.

这篇关于使用参数的 npgsql 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆