简体   繁体   English

带有多个语句的Npgsql命令

[英]Npgsql Command with Multiple Statements

In Npgsql V2, I could use the following code to update a record, and return the updated record values using a single Npgsql command. 在Npgsql V2中,我可以使用以下代码更新记录,并使用单个Npgsql命令返回更新的记录值。

The command.CommandText property contains both an UPDATE statement and also a SELECT statement. command.CommandText属性包含UPDATE语句和SELECT语句。 The idea being that when command.ExecuteReader is called both commands will run, but the results from the SELECT command will be returned (since it was the last command). 我们的想法是,当调用command.ExecuteReader时,两个命令都将运行,但将返回SELECT命令的结果(因为它是最后一个命令)。

After upgrading to Npgsql version 3.0.3.0 the value in the datareader (from the SELECT statement) is still the original value, and not the updated one (the Return dr("action") line in the code). 升级到Npgsql版本3.0.3.0后,datareader中的值(来自SELECT语句)仍然是原始值,而不是更新的值(代码中的Return dr("action")行)。 I have tried every different IsolationLevel and they all give the same results (as though the SELECT statement is not seeing the updated value from the INSERT statement). 我尝试了每个不同的IsolationLevel,它们都给出了相同的结果(就好像SELECT语句没有看到INSERT语句中的更新值)。 The value is properly updated in the database (if I re-query the record it has the updated value). 该值在数据库中正确更新(如果我重新查询记录,它具有更新的值)。

I can split this and use two separate NpgsqlCommand (one for the INSERT, and a second for the SELECT), but I don't want to create a second round-trip to the server. 我可以拆分它并使用两个单独的NpgsqlCommand(一个用于INSERT,另一个用于SELECT),但我不想创建第二次往返服务器。

This is a simplified function, the purpose of the real function is to update a record on the DB server, and then update the object in the application with any other fields that the server updated (such as the "last_updated" timestamp field which is updated on the server each time a record is updated). 这是一个简化的函数,实际功能的目的是更新数据库服务器上的记录,然后使用服务器更新的任何其他字段更新应用程序中的对象(例如更新的“last_updated”时间戳字段每次更新记录时在服务器上)。

Is there a way to make this work with Npgsql V3.0.3.0? 有没有办法使这个工作与Npgsql V3.0.3.0?

Public Function UpdateRecordExample(id As Guid, newAction As String) As String
        Using conn As New NpgsqlConnection("Connection String Here")
            Using trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted)
                Dim command = conn.CreateCommand
                command.UpdatedRowSource = UpdateRowSource.FirstReturnedRecord
                command.CommandText = "Update pm.action_item SET action=@action WHERE id=@id; SELECT * FROM pm.action_item WHERE id=@ID;"
                command.Parameters.Add(New NpgsqlParameter("id", id))
                command.Parameters.Add(New NpgsqlParameter("action", newAction))
                Using dr = command.ExecuteReader
                    If dr.Read Then
                        Return dr("action") 'This is still the original value and not "newAction"
                    Else
                        Throw New DBConcurrencyException
                    End If
                End Using
            End Using
        End Using
    End Function

请注意,此问题已在Npgsql 3.1中得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM