.NET Oracle SQL 命令未执行
.NET Oracle SQL Command not executing
我有一个函数可以从存储 rtf 字符串的 Oracle 数据库中提取 CLOB 列。
public static string GetRTFNoteParsed(string limsNoteNum, string radioEnv)
{
var rtfNote = "";
string oradb = GetDBconfig(radioEnv);
OracleConnection conn = new OracleConnection(oradb);
using (conn)
{
try
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
if (limsNoteNum != null)
{
//rtf notes are not stored as plain text. This regex takes it out
cmd.CommandText = "SELECT REGEXP_REPLACE(REGEXP_REPLACE(NOTE_CONTENTS, '\(fcharset|colortbl)[^;]+;', ''), '(\[^ ]+ ?)|[{}]', '') FROM LIMS_NOTES WHERE NOTE_ID = " + limsNoteNum;
//cmd.CommandText = "SELECT NOTE_CONTENTS FROM LIMS_NOTES WHERE NOTE_ID = 86253";
}
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
rtfNote = dr.GetValue(0).ToString();
}
}
if (conn != null)
{
conn.Dispose();
}
}
catch (Exception e4)
{
log.Error("Error with data pulling", e4);
return (null);
}
finally
{
if (conn != null)
{
conn.Dispose();
}
}
return rtfNote.ToString();
}
}
我知道问题与命令有关,因为注释掉的命令有效(但仍以 rtf 格式)。我知道导致我出现问题的命令在 SQL Developer 中运行良好,被注释掉的命令也是如此。但是当我在我的 .NET 应用程序中 运行 它时,我回来了 "ORA-01002: fetch out of sequence." 有什么想法吗?
我无法重现您的错误 ORA-01002: fetch out of sequence
,但我确实收到错误 ORA-12725: unmatched parentheses in regular expression
运行 您的代码。解决方法是为您的查询使用 C# 逐字字符串,换句话说,替换
cmd.CommandText = "SELECT REGEXP_REPLACE(REGEXP_REPLACE(......";
和
cmd.CommandText = @"SELECT REGEXP_REPLACE(REGEXP_REPLACE(......";
使用逐字字符串会阻止 C# 处理字符串中的反斜杠,特别是将 \
替换为 \
。
此外,如果我不建议您使用参数化 SQL 而不是连接字符串,那将是我的疏忽,因为您的代码目前容易受到 SQL 注入的攻击。替换
cmd.CommandText = @"SELECT ... FROM LIMS_NOTES WHERE NOTE_ID = " + limsNoteNum;
和
cmd.CommandText = @"SELECT ... FROM LIMS_NOTES WHERE NOTE_ID = :note_id";
并添加行
cmd.Parameters.Add(new OracleParameter("note_id", OracleDbType.Varchar2, limsNoteNum, ParameterDirection.Input));
我有一个函数可以从存储 rtf 字符串的 Oracle 数据库中提取 CLOB 列。
public static string GetRTFNoteParsed(string limsNoteNum, string radioEnv)
{
var rtfNote = "";
string oradb = GetDBconfig(radioEnv);
OracleConnection conn = new OracleConnection(oradb);
using (conn)
{
try
{
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
if (limsNoteNum != null)
{
//rtf notes are not stored as plain text. This regex takes it out
cmd.CommandText = "SELECT REGEXP_REPLACE(REGEXP_REPLACE(NOTE_CONTENTS, '\(fcharset|colortbl)[^;]+;', ''), '(\[^ ]+ ?)|[{}]', '') FROM LIMS_NOTES WHERE NOTE_ID = " + limsNoteNum;
//cmd.CommandText = "SELECT NOTE_CONTENTS FROM LIMS_NOTES WHERE NOTE_ID = 86253";
}
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
rtfNote = dr.GetValue(0).ToString();
}
}
if (conn != null)
{
conn.Dispose();
}
}
catch (Exception e4)
{
log.Error("Error with data pulling", e4);
return (null);
}
finally
{
if (conn != null)
{
conn.Dispose();
}
}
return rtfNote.ToString();
}
}
我知道问题与命令有关,因为注释掉的命令有效(但仍以 rtf 格式)。我知道导致我出现问题的命令在 SQL Developer 中运行良好,被注释掉的命令也是如此。但是当我在我的 .NET 应用程序中 运行 它时,我回来了 "ORA-01002: fetch out of sequence." 有什么想法吗?
我无法重现您的错误 ORA-01002: fetch out of sequence
,但我确实收到错误 ORA-12725: unmatched parentheses in regular expression
运行 您的代码。解决方法是为您的查询使用 C# 逐字字符串,换句话说,替换
cmd.CommandText = "SELECT REGEXP_REPLACE(REGEXP_REPLACE(......";
和
cmd.CommandText = @"SELECT REGEXP_REPLACE(REGEXP_REPLACE(......";
使用逐字字符串会阻止 C# 处理字符串中的反斜杠,特别是将 \
替换为 \
。
此外,如果我不建议您使用参数化 SQL 而不是连接字符串,那将是我的疏忽,因为您的代码目前容易受到 SQL 注入的攻击。替换
cmd.CommandText = @"SELECT ... FROM LIMS_NOTES WHERE NOTE_ID = " + limsNoteNum;
和
cmd.CommandText = @"SELECT ... FROM LIMS_NOTES WHERE NOTE_ID = :note_id";
并添加行
cmd.Parameters.Add(new OracleParameter("note_id", OracleDbType.Varchar2, limsNoteNum, ParameterDirection.Input));