来自.Net的Oracle参数化查询

时间:2013-04-26 22:16:23

标签: .net sql oracle c#-4.0

所以这是我得到的错误:ORA-01036:非法变量名称/编号

我正在尝试用这些值更新表格。值不为null,参数中的每个变量都包含值。

查询在Oracle中运行良好(没有参数),但是当我添加参数时,它会给出ORA-01036:非法变量名称/编号。

我几乎尝试了一切,但没有运气!!

using (OracleConnection con = new OracleConnection(cs))
            {
            const string query =
                   @"UPDATE ASSOCIATED_CENTERS_INFO
SET DIS_ASSOCIATED_BY = (SELECT ID FROM VC_ADMINISTRATORS WHERE USERNAME = ':USER'),
DIS_ASSOCIATION_DATE = SYSDATE,
ACTIVE_YN = 'N', 
NOTES = CONCAT(NOTES, '<br>' || SYSDATE || ' ' || (SELECT FIRSTNAME || ',' || LASTNAME FROM VC_ADMINISTRATORS WHERE USERNAME = ':USER') || ': ' || ':COMMENT')
WHERE OSC_ID = :OSCID AND ASSOCIATED_OSC_ID = :ASSOCIATEDOSCID;";

                OracleCommand cmd = new OracleCommand(query, con);
                cmd.CommandType=CommandType.Text;

                cmd.Parameters.AddWithValue(":USER", userName);
                cmd.Parameters.AddWithValue(":COMMENT", comment);
                cmd.Parameters.AddWithValue(":OSCID", selectedIdOnMap);
                cmd.Parameters.AddWithValue(":ASSOCIATEDOSCID", selectedIdOnListBox);
                con.Open();
                cmd.ExecuteNonQuery();

}

2 个答案:

答案 0 :(得分:2)

删除围绕某些参数添加的单引号:

':USER', ':COMMENT' ...

答案 1 :(得分:0)

将所有表名和列名放在引号中:“MY_TableName”: 像:

     @"UPDATE ""ASSOCIATED_CENTERS_INFO""
   SET ""DIS_ASSOCIATED_BY "" = .......

并从参数中删除引号。