我被要求做一些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)的連接。
關於您對詳細問題的理解,我認爲它有助於其他開發人員瞭解問題的開發環境和性質。這可能導致徹底/徹底改變如何處理/解決問題的過程。 –