使用vb.net将变量设置为字段名sql

使用vb.net将变量设置为字段名sql,vb.net,postgresql,Vb.net,Postgresql,根据这些数据: id | month | 2015 | 2014 | 2013 1 | january | 2 | 4 | 6 2 | february | 10 | 12 | 14 3 | march | 16 | 18 | 20 我这里有vb.net代码 Dim asd As Double = 2015 Dim msg As Double sql = "select " & asd & " from tbl_coll_p

根据这些数据:

id | month    | 2015 | 2014 | 2013
1  | january  | 2    | 4    | 6  
2  | february | 10   | 12   | 14
3  | march    | 16   | 18   | 20
我这里有vb.net代码

Dim asd As Double = 2015
Dim msg As Double

sql = "select " & asd & " from tbl_coll_penalty where month = 'february'"
sda = New NpgsqlDataAdapter(sql, pgConnection)
sda.Fill(DS, "t")

msg = DS.Tables("t").Rows(0)(0).ToString()
MessageBox.Show(msg)

我用这个代码得到了错误的答案,因为这个代码的答案是“2015”,但我希望答案是“10”。有人能帮我找到正确的代码吗?

在PostgreSQL中,您的列名“2015”被解释为文本值。因此,当您提交查询时:

SELECT 2015 FROM tbl_coll_penalty ...
您只需返回值即可

在SQL标准中,列标识符不能以数字(0..9)开头。检查文档。要让PostgreSQL将字符串“2015”解释为列名,应将其双引号:

sql = "select """ & asd & """ from tbl_coll_penalty where month = 'february'"

先生,在我的2015年postgre中,字段数据类型为双精度。我发现错误函数quote_ident(integer)不存在
quote_ident()
确实需要一个字符串,因此无法工作。唯一明智的选择是将列名双引号引起来。请参阅更新的答案。一点也不漂亮。如果可以,请更改列名。我无法更改我的列名,因为它们是我使用该结构的原因之一。@MichaelBeray,您应该执行显式强制转换'select quote_ident(2015::text);`或者您可以使用格式功能
select format('select%I from foo',2015)
Dim asd As Double=2014 Dim msg As Double sql=“从tbl_coll_惩罚中选择”“2014”“,其中month='May'”sda=New NpgsqlDataAdapter(sql,pgConnection)sda.Fill(DS,“t”)msg=DS.Tables(0).Rows(0).Item(0)MessageBox.Show(msg)为什么不添加一个字段名“year”以及每年下数据的另一个字段名,这样您就可以像这样进行查询>>>从tbl_coll_中选择数据,其中月份为'二月',年份为''&asd&'?'???