ORA-01745执行插入时

本文关键字:插入 执行 ORA-01745 | 更新日期: 2023-09-27 18:02:55

下面的ExecuteNonQuery返回sql错误:ORA-01745: invalid host/bind variable name,但是正如您所看到的,这些参数名不是保留字。为什么会出现这个错误?(我试图重命名我所有的参数)。

public bool AddArticle(ArticleModel articleModel)
        {
            bool result = false;
            using (OracleConnection conn = new OracleConnection(BasicConnection.connectionStringOracle))
            using (OracleCommand command = conn.CreateCommand())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    bool inserted = false;
                    string articleGuid = System.Guid.NewGuid().ToString().Replace("-", "");
                    command.CommandText = "INSERT INTO ARTICLE (ARTICLE.ID,USER_ID)" +
                        "VALUES(:aid,:auserId)";
                    command.Parameters.Add("aid", OracleDbType.Varchar2).Value = articleGuid;
                    command.Parameters.Add("auserId", OracleDbType.Varchar2).Value = articleModel.UserId;
                    inserted = (command.ExecuteNonQuery() == 1);
                    if (!inserted)
                    {
                        trans.Rollback();
                        return false;
                    }

                    command.CommandText = "INSERT INTO ARTICLE_CONTENT (LANG_ID,ARTICLE_ID,A_TITLE,A_CONTENT,ARTICLE_STATES_ID) "+
                        "VALUES(:alangId:, :aarticleId, :aatitle, :aacontent, :astateId)";
                    command.Parameters.Add("alangId", OracleDbType.Varchar2).Value = articleModel.Content.LangId;
                    command.Parameters.Add("aarticleId", OracleDbType.Varchar2).Value = articleGuid;
                    command.Parameters.Add("aatitle", OracleDbType.Varchar2).Value = articleModel.Content.ATitle;
                    command.Parameters.Add("aacontent", OracleDbType.Varchar2).Value = articleModel.Content.AContent;
                    command.Parameters.Add("astateId", OracleDbType.Varchar2).Value = articleModel.Content.ArticleStatesId;
                    inserted = (command.ExecuteNonQuery() == 1);
                    if (!inserted)
                    {
                        trans.Rollback();
                        return false;
                    }
                    trans.Commit();
                    result = true;
                }
            }
            return result;
        }

影响的表:

CREATE TABLE Article
  (
    id         VARCHAR2 (32) DEFAULT sys_guid() NOT NULL ,
    User_id    VARCHAR2 (32) NOT NULL ,
    created_at DATE DEFAULT sysdate ,
    updated_at DATE DEFAULT sysdate
  ) ;
ALTER TABLE Article ADD CONSTRAINT Article_PK PRIMARY KEY ( id ) ;
CREATE TABLE Article_Content
  (
    id         VARCHAR2 (32) DEFAULT sys_guid() NOT NULL ,
    Lang_id    VARCHAR2 (32) NOT NULL ,
    Article_id VARCHAR2 (32) NOT NULL ,
    a_title    VARCHAR2 (25) DEFAULT 'No-Title' NOT NULL ,
    a_content CLOB ,
    Article_States_id VARCHAR2 (32) NOT NULL
  ) ;
ALTER TABLE Article_Content ADD CONSTRAINT Article_Content_PK PRIMARY KEY ( id ) ;
ALTER TABLE Article_Content ADD CONSTRAINT Article_Content__UN UNIQUE ( a_title ) ;
CREATE TABLE Article_States
  (
    id      VARCHAR2 (32) DEFAULT sys_guid() NOT NULL ,
    a_state VARCHAR2 (50) DEFAULT 'ERROR'
  ) ;
ALTER TABLE Article_States ADD CONSTRAINT Article_States_PK PRIMARY KEY ( id ) ;

ORA-01745执行插入时

我猜是

后面的冒号
"VALUES(:alangId:, 

只是个打字错误。