gpt4 book ai didi

c# - 函数仅在使用 System.Data.OracleClient 时保持无效,通过 SQL Developer 工作

转载 作者:太空宇宙 更新时间:2023-11-03 11:13:50 25 4
gpt4 key购买 nike

我正在使用 C# 和 System.Data.OracleClient 向数据库添加函数。这适用于大多数功能,除了一个。该函数已创建,但其状态为 INVALID。检查此无效状态的原因后,我注意到我可以在 SQL Developer 中简单地编译该函数,但不能从我的 C# 应用程序中编译。

知道为什么使用 .NET 和 SQL Developer 有区别吗?

这是我用的函数

string sql =
@"CREATE OR REPLACE FUNCTION MYUSER.TEMPJOINSTRINGS
( P_CURSOR SYS_REFCURSOR,
P_DEL VARCHAR2 := ', '
) RETURN VARCHAR2
IS
L_VALUE VARCHAR2(32767);
L_RESULT VARCHAR2(32767);
BEGIN
LOOP
FETCH P_CURSOR INTO L_VALUE;
EXIT WHEN P_CURSOR%notfound;
IF L_RESULT IS NOT NULL THEN
L_RESULT := L_RESULT || P_DEL;
END IF;
L_RESULT := L_RESULT || L_VALUE;
END LOOP;
RETURN L_RESULT;
END;";

try
{
using (OracleConnection connection = new OracleConnection(@"Data source=TEST10;User Id=MYUSER;Password=MYPASS;"))
{
connection.Open();
DbCommand cmd = connection.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
connection.Close();
return true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

然后我执行

ALTER FUNCTION MYUSER.TEMPJOINSTRINGS COMPILE

但编译只能在 SQL Developer 中进行,而不能在我的 C# 应用程序中进行。

最佳答案

我猜测 .NET 中的字符串是 bieng 格式为 windows syle ie。\r\n 换行。

试试这个

    sql = sql.Replace("\r", "");

在构建函数字符串之后。

如果您这样做了,您可以快速确认这一点:

SQL> show errors function TEMPJOINSTRINGS
Errors for FUNCTION TEMPJOINSTRINGS:

LINE/COL ERROR
-------- -----------------------------------------------------------------
1/26 PLS-00103: Encountered the symbol "" when expecting one of the
following:
( return compress compiled wrapped

SQL> select status from user_objects where object_name = 'TEMPJOINSTRINGS';

STATUS
-------
INVALID

SQL> select text, dump(text) from user_source where name = 'TEMPJOINSTRINGS' and line = 1;

TEXT
--------------------------------------------------------------------------------
DUMP(TEXT)
--------------------------------------------------------------------------------
FUNCTION TEMPJOINSTRINGS
Typ=1 Len=26: 70,85,78,67,84,73,79,78,32,84,69,77,80,74,79,73,78,83,84,82,73,78,
71,83,13,10

13,10 最后是\r\n

关于c# - 函数仅在使用 System.Data.OracleClient 时保持无效,通过 SQL Developer 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234505/

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