gpt4 book ai didi

vb.net - 尝试将 Vb.net 与 PostgreSQL 数据库集成时 NpgsqlDatareader 的问题

转载 作者:行者123 更新时间:2023-11-29 14:01:08 27 4
gpt4 key购买 nike

所以我对 PostgreSQL 和 VB.net 相当陌生(刚刚开始阅读)。我已经完成研究并使用 Npgsql.dll 和 Mono.Security.Protocol.Tls.dll 作为连接到数据库的引用。

|barcode|item|type|received|removed|on_hand|

|ien0001|thing1|type1| 1 | 2 | 3 |

|ien0002|thing2|type2| 4 | 5 | 6 |

|ien0003|thing1|type1| 7 | 8 | 9 |

我的表的例子

现在我遇到的问题是,当我使用 myReader.GetString(0) 时,返回的值始终为 0。现在,如果我将此行硬编码到下面,我将获得数据库中的实际值。我不确定我是否在做一些愚蠢的事情,或者我是否在我的头上。任何建议都会很棒。我希望问题很清楚。如果没有,请告诉我,我会尝试重新措辞。这也是我第一次发帖,如果我的格式很糟糕,请告诉我。

谢谢

此外,如果我不对条形码进行硬编码(没有双关语意),则永远不会执行 if 语句。

此外,我忘了提及我要完成的工作。我正在尝试使用名为代码的 VB 变量查询数据库。然后根据查询的结果(最多只有一个结果)从列中提取一个值并将其保存到另一个变量中。

如果有其他方法,请告诉我。谢谢

mySQLString = "SELECT On_Hand FROM total_inventory WHERE Barcode = 'ien0002' ;"

 code = "'ien0001'"   'code is a string
myConnection.ConnectionString = "Server=###.###.###.###;Port=####;Database=inventory;User Id=inventory_user;Password=$$$$$$;"
myConnection.Open()
myCommand = New NpgsqlCommand(mySQLString, myConnection)
myCommand.ExecuteNonQuery()
myReader = myCommand.ExecuteReader
If myReader.Read() Then
CurrentQty = CInt(myReader.GetString(0))
MsgBox(":" & CurrentQty)
End If
myReader.Close()
MsgBox(CurrentQty)
myConnection.Close()

最佳答案

去掉

两边的单引号
code = "ien0001" 

并使用参数化查询。框架代码足够智能,可以识别您传递的是一个字符串,并会在您的字符串周围放置正确的引号,而无需手动操作(并出错)

所以如果我的假设是正确的那么你写

Dim code = "ien0001" 
mySQLString = "SELECT On_Hand FROM total_inventory WHERE Barcode = :code;"
myConnection.ConnectionString = "Server=###.###.###.###;Port=####;Database=inventory;User Id=inventory_user;Password=$$$$$$;"
myConnection.Open()
myCommand = New NpgsqlCommand(mySQLString, myConnection)
myCommand.Parameters.AddWithValue(":code", code)
myCommand.ExecuteNonQuery()
myReader = myCommand.ExecuteReader
If myReader.Read() Then
CurrentQty = CInt(myReader.GetString(0))
MsgBox(":" & CurrentQty)
End If
myReader.Close()
MsgBox(CurrentQty)
myConnection.Close()

关于vb.net - 尝试将 Vb.net 与 PostgreSQL 数据库集成时 NpgsqlDatareader 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17090626/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com