我被要求做一些PostgreSQL的研究和运行后的代码段 系统返回两个条目(未命名Portal 1和Unnamed Portal 2)当我们读DBDataReader对象时, 但是我想从这两个门户读取所有recods,然后用我的业务对象加载信息,因为每个门户在表中有5个以上的记录。从PostgreSQL数据库检索数据使用ADO.Net“System.Data.Odbc”(VB.Net)
在浏览了互联网上的各种网页之后,我发现有一个第三方库(“NpgSQL”> NpgsqlDataReader)提供了 这个功能,它是免费的。我已将他们的图书馆插入我的解决方案,并按照期望进行工作。然而, 我不想使用这个第三方软件,只是想知道是否有一种方法,我可以从这两个门户读取记录,然后 这将是伟大的。
请注意,您可以给我们一个在c#中的解决方案,因为我熟悉这一点。谢谢。函数来检索数据 -
,我现在使用的检索数据
Dim oConnection As DbConnection = DbProviderFactories.GetFactory(CONST_PROVIDER_ODBC).CreateConnection
oConnection.ConnectionString = "Valid connection string goes here"
oConnection.Open()
' Start a transaction as it is required to work with cursors in PostgreSQL
Dim tran As DbTransaction = oConnection.BeginTransaction
' Define a command to call stored procedure show_cities_multiple
Dim command As DbCommand = DbProviderFactories.GetFactory("System.Data.Odbc").CreateCommand
command.CommandText = "SELECT MyTestFunction()"
command.CommandType = CommandType.StoredProcedure
command.Connection = oConnection
command.Transaction = tran
' Execute the stored procedure and obtain the first result set
Dim dr As DbDataReader = command.ExecuteReader()
' Output the rows of the first result set
While dr.Read()
'This is where system show Unnamed Portal 1 or Unnamed portal 2 at the second time.
MsgBox(String.Format("{0}", dr(0)))
End While
tran.Commit()
代码段。
CREATE OR REPLACE FUNCTION MyTestFunction() RETURNS SETOF refcursor AS $$
DECLARE
ref1 refcursor; -- Declare cursor variables
ref2 refcursor;
BEGIN
OPEN ref1 FOR SELECT * FROM MyTable1;
RETURN NEXT ref1; -- Return the cursor to the caller
OPEN ref2 FOR SELECT * FROM MyTable2;
RETURN NEXT ref2; -- Return the cursor to the caller
END;
$$ LANGUAGE plpgsql;
环境我正在上以下内容: -
32位计算机上。
的Visual Studio 2010 + SP1
ODBC Prodiver:PostgreSQL的统一9.01.02.00
ADO.Net(System.Data.Odbc)
当然,我期待着听到任何反馈,你的帮助将是非常感激。
如果您使用C#,我强烈建议您使用nPgSQL。它比* psqlODBC *支持得更好,并且不再比它自己的psqlODBC更“第三方”。它们都是由不同团队维护的PostgreSQL核心之外的项目。顺便说一句,谢谢你的好,详细的问题。 –
谢谢Craig的意见。 我必须承认一个错误,因为我已经在我的解决方案中使用了npgSQL库,并且在这个问题中错误地写了“PostgreSQL Unicode”。 –
**感谢您的Craig提供的输入。**
我已经设法在我的解决方案中实现npgSQL库,它似乎工作正常。必须在我的数据访问层(DAL)中进行一些更改,因为您可能已经注意到我们正在使用DBProviderFactory建立两个不同数据库(SQL Server和PostgreSQL)的连接。
关于您对详细问题的理解,我认为它有助于其他开发人员了解问题的开发环境和性质。这可能导致彻底/彻底改变如何处理/解决问题的过程。 –