以下ExecuteNonQuery
返回sql错误:ORA-01006: bind variable does not exist
。我检查了查询中定义的所有绑定参数,然后出现了什么问题?
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 ) ;
答案 0 :(得分:0)
好的,我发现了。
我需要在第一个command.Parameters
之后清除command.ExecuteNonQuery()
,否则第二个将尝试绑定在第一个之后定义的参数。 (显然我不想要那个,因为第二个插入没有:aid和:auserId参数)
command.Parameters.Clear();