gpt4 book ai didi

c# - 在 C# 中调用 Oracle 存储过程

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:05 26 4
gpt4 key购买 nike

我正在尝试从 C# 程序调用 Oracle 存储过程。我正在使用 SYS_REFCURSOR 作为存储过程的输出。当我到达该行时出现无效的 SQL 错误

OracleDataReader reader = cmd.ExecuteReader() 

在我的 C# 程序中。我不明白为什么我会收到这个无效的 SQL 错误。

这是 C# 代码:

private void button1_Click(object sender, EventArgs e)
{
string custname;
int custnbr;

List<Customer> customers = new List<Customer>();

string oradb = "User Id=XXXXX;Password=XXXXX;Data Source=IP:PORT/xxxx;Pooling=false;";
OracleConnection conn = new OracleConnection(oradb);

try
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "PROCEDURE_TEST";

OracleParameter oraP = new OracleParameter();
oraP.ParameterName = "R_RECORDSET";
oraP.OracleDbType = OracleDbType.RefCursor;
oraP.Direction = System.Data.ParameterDirection.Output;

cmd.Parameters.Add(oraP);
cmd.CommandType = CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
custnbr = reader.GetInt32(0);
custname = reader.GetString(1);
Customer custTemp = new Customer(custnbr, custname);
customers.Add(custTemp);
}

foreach (var cust in customers)
{
textBox1.AppendText("Customer Number: " + cust.custnbr + "\t");
textBox1.AppendText("Customer Name: " + cust.custname + "\r\n");
}
}

catch(Exception ex)
{
textBox1.AppendText(ex.ToString());
conn.Close();
}
}

这是 Oracle 存储过程:

create or replace PROCEDURE PROCEDURE_TEST 
( R_RECORDSET OUT SYS_REFCURSOR) AS
BEGIN
OPEN R_RECORDSET FOR
SELECT POTCHARGECATEGORY, POTCHARGECODE, POTCHARGEDESCRIPTION,
POTCHARGEBASEAMT, SUM(POTCHARGEQTY), SUM(POTCHARGEAMOUNT)

FROM riowner.ccum_customer customer

WHERE ic.collection_Datetime =
TO_DATE('30-SEP-2015 23:59:59','DD-MON-YYYY HH24:MI:SS')

GROUP BY POTCHARGECATEGORY, POTCHARGECODE, POTCHARGEDESCRIPTION,
POTCHARGEBASEAMT;
END PROCEDURE_TEST;

最佳答案

cmd.CommandType = CommandType.Text; 

应该是

cmd.CommandType = CommandType.StoredProcedure;

关于c# - 在 C# 中调用 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com