C#和PostgreSQL [英] C# and PostgreSQL

查看:2089
本文介绍了C#和PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以告诉我的工作例如利用PLSQL返回到C#code光标? 我发现了很多例子来说明如何填充DataSet中返回的数据,但我无法找到如何使用游标,DataReader的,结果我{不愿透露姓名的门户网站}。

Can any one show me working example of using cursor returned from PLSQL to c# code? I found many examples showing how to fill dataSet with returned data but I cannot find how to use that cursor with DataReader, as a result I have {unnamed portal}.


NpgsqlTransaction tr = (NpgsqlTransaction) Connection.BeginTransaction();
NpgsqlCommand cursCmd = new NpgsqlCommand("someStoredProcedure(:inRadius)",
                                         (NpgsqlConnection) Connection);
cursCmd.Transaction = tr;
NpgsqlParameter rf = new NpgsqlParameter("ref", 
                                         NpgsqlTypes.NpgsqlDbType.Refcursor);
rf.Direction = ParameterDirection.InputOutput;
cursCmd.Parameters.Add(rf);


我有什么写在这里使用NpgsqlDataReader myReader;正确。


what i have to write here to use NpgsqlDataReader myReader; correctly.

tr.Commit();

当我说'取'它的工作原理,但它是不适合的SQL命令之后。

when I wrote 'fetch' after sql command it works but it is not suitable.

日Thnx提前

推荐答案

我有我的问题,一些答案。

I have got some answers on my question.

问题:我已经存储PLSQL过程返回REFCURSOR。 我必须让与的DataReader返回的数据。 但是,当我添加的参数DB返回

Problem: I have stored PLSQL procedure which returns refCursor. I have to get returned data with datareader. But When I added parameters db returned

要遍历所有返回的数据我有写我的code这样:

To walk through all returned data I have to write my code so:



NpgsqlTransaction tr = (NpgsqlTransaction) Connection.BeginTransaction();
NpgsqlCommand cursCmd = new NpgsqlCommand("someStoredProcedure",
                                         (NpgsqlConnection) Connection);
cursCmd.Transaction = tr;
NpgsqlParameter rf = new NpgsqlParameter("ref", 
                                         NpgsqlTypes.NpgsqlDbType.Refcursor);
rf.Direction = ParameterDirection.InputOutput;
cursCmd.Parameters.Add(rf);

NpgsqlParameter param2 = new NpgsqlParameter("param1", 
                                         NpgsqlTypes.Int32);
rf.Direction = ParameterDirection.Input;
cursCmd.Parameters.Add(param2);
  NpgsqlDataReader r = cmd.ExecuteReader();                

                while (r.Read())
                {                    
                        ;// r.GetValue(0);
                }
                r.NextResult();                
                while(r.Read())
                {
                    ;
                }

tr.Commit();

你应该注意到,你有没有在SQL像FUNC写你的参数(:参数1)

you should notice that you haven't write your parameters in sql like func(:param1)

如果你在你的函数的参数,只分配函数名CommandText属性和参数添加到NpgsqlCommand.Parameters集合如常。 Npgsql将采取的正确结合的参数的照顾。

If you have parameters in your function, assign only the function name to the CommandText property and add parameters to the NpgsqlCommand.Parameters collection as usual. Npgsql will take care of binding your parameters correctly.

但现在我还有一个问题。当我通过只是一个输出参数为我的CommandText。 因此,我不得不域其中之一是0 {我的第一个输出参数}另一个是 在oracle中我可以REFCURSOR参数直接转换为DataReader的,但在PostgreSQL中我不能。

But now I have another problem. When I pass just another output parameter to my commandtext. As a result I have to fields one of them is 0{my first output param} another one is In oracle i can directly convert RefCursor parameter to datareader but in postgresql i cannot.

感谢您的关注

这篇关于C#和PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆