簡體   English   中英

在 VB.Net 中,為什么 OleDataReader GetDecimal() function 只返回 integer 部分?

[英]In VB.Net, why OleDataReader GetDecimal() function returns only integer part?

當我在VB.Net (Visual Studio 2019) 上運行以下代碼時,得到 789,但從未收到 789.1234。 小數部分總是丟失!

Imports System.Data.OleDb

Dim cs = "Provider=PostgreSQL OLE DB Provider;" _
       & "DataSource=127.0.0.1;" _
       & "location=extraits;" _
       & "User ID=postgres;" _
       & "Password='Sp1r1tus'"

Dim oleconx = New OleDbConnection(cs)
Dim cmd = New OleDbCommand("SELECT 789.1234 as account", oleconx)
Dim rxSheet = cmd.ExecuteReader()
Dim n = rxSheet.GetOrdinal("account")
Dim dAccount = rxSheet.GetDecimal(n)

如果我使用以下代碼也是一樣的

Dim dAccount = rxSheet("account")

變量dAccount包含 789 ! 小數部分丟失!

在實際情況下(以前的代碼只是解釋問題的簡單代碼),我發現使用相同代碼獲得完整十進制數的唯一解決方案是更改 SQL SELECT 命令

SELECT account * 1000 as account

並編寫以下 VB.Net 代碼

Dim dAccount as Decimal = rxSheet("account") / 1000

但這是一種解決方法。

我剛剛使用標准 Setup exe 程序安裝了 Postgres 14.1。 在此版本之前,我在 Windows 10 上的同一台 PC 上使用版本 8.4。也許某些 DLL 沒有安裝在正確的位置(如 libpq.dll)。

這個問題在 8.4 版本中已經存在。

一些身體可以幫助我嗎?

我已經在 PostGres 上發布了這個錯誤,但他們回復如下

PostgreSQL 不提供內置的 OLE DB 提供程序。 您需要聯系您的 OLE DB 驅動程序的作者。

我的問題有解決方案嗎?

誰負責 PostGres 或 Microsoft?

我已經嘗試過使用 Intellisoft OLEDB Provider for PostgreSQL (PGNP) 的場景,它按預期工作。 我不得不稍微更改連接字符串,並添加包含 rxSheet.Read() 的行。 這是工作代碼的副本:

Dim cs = "Provider=PGNP.1;" _
            & "Data Source=127.0.0.1;" _
            & "Initial Catalog=postgres;" _
            & "User ID=postgres;" _
            & "Password=123!"

    Dim oleconx = New OleDbConnection(cs)
    oleconx.Open()
    Dim cmd = New OleDbCommand("SELECT 789.1234 as account", oleconx)
    Dim rxSheet = cmd.ExecuteReader()
    Dim n = rxSheet.GetOrdinal("account")
    While rxSheet.Read()
        Dim dAccount = rxSheet.GetDecimal(n)
        Console.WriteLine("dAccount={0}", dAccount)
    End While

結果如下:dAccount=789.1234

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM