我编写了一个小程序来将一些记录插入到 oracle 数据库中。如果我直接连接到 oracle,程序运行良好。但是,如果我尝试通过 ODBC 连接,该程序将无法运行。它能够执行SELECT
语句,但不能执行INSERT
语句。
当我开发程序时,我在我的 NHibernate 配置中使用了这些设置:
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="connection.connection_string">
Data Source=192.168.1.43:1521/xxxx;User ID=xxxx;Password=xxxx
</property>
<property name="connection.driver_class">
NHibernate.Driver.OracleClientDriver
</property>
<property name="hbm2ddl.keywords">auto-quote</property>
通过这些设置,程序可以工作。
对于生产,我使用以下设置:
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="connection.connection_string">
DSN=TRUST;Server=oracle;UID=xxxx;PWD=xxxx
</property>
<property name="connection.driver_class">
NHibernate.Driver.OdbcDriver
</property>
<property name="hbm2ddl.keywords">auto-quote</property>
使用生产设置,当我尝试插入数据时出现此异常:
NHibernate.Exceptions.GenericADOException: could not insert: [Domain.Phoenix.StructureAssign#35159][SQL: INSERT INTO STRUCTURE_ASSIGN (CLIENT_ID, STRUCTURE, NAME_CODE, START_DATE, FINISH_DATE, MEMO_NUMBER, ALTERNATE, ALTERNATE_NAME_CODE, ADMIN_NAME, AUDIT_NAME, VALID_DATE, AUDIT_ACTION, DIRECTOR_CLASS, STAT_SEQ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] ---> System.Data.Odbc.OdbcException
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)
at System.Data.Odbc.OdbcCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
--- End of inner exception stack trace ---
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.StatelessSessionImpl.Insert(String entityName, Object entity)
at NHibernate.Impl.StatelessSessionImpl.Insert(Object entity)
at Domain.Repositories.StatelessRepository.Add[T](T model) in c:\Users\Owner\Dev\\Domain\Repositories\StatelessRepository.cs:line 112
at PatchImport.Program.InsertBeneficiaries() in c:\Users\Owner\Dev\PatchImport\Program.cs:line 232
at PatchImport.Program.Main(String[] args) in c:\Users\Owner\Dev\PatchImport\Program.cs:line 60
内部异常没有附加任何消息。
我查看了 NHibernate 生成的 SQL。使用开发设置,为输入生成的 SQL 如下所示:
NHibernate: select STRUCTURE_SEQ.nextval from dual
NHibernate: INSERT INTO STRUCTURE_ASSIGN
(CLIENT_ID, STRUCTURE, NAME_CODE, START_DATE, STAT_SEQ)
VALUES (:p0, :p1, :p2, :p3, :p4);
:p0 = 'MAST17' [Type: String (6)],
:p1 = 'BENEFICIARY' [Type: String (11)],
:p2 = 27845 [Type: Int64 (0)],
:p3 = 28/10/2008 00:00:00 [Type: DateTime (0)],
:p4 = 42830 [Type: Int32 (0)]
但是,使用生产设置生成的 SQL 如下所示:
NHibernate: select STRUCTURE_SEQ.nextval from dual
NHibernate: INSERT INTO STRUCTURE_ASSIGN
(CLIENT_ID, STRUCTURE, NAME_CODE, START_DATE, STAT_SEQ)
VALUES (?, ?, ?, ?, ?);
p0 = 'SCAR03' [Type: String (6)],
p1 = 'BENEFICIARY' [Type: String (11)],
p2 = 21525 [Type: Int64 (0)],
p3 = 07/11/1977 00:00:00[Type: DateTime (0)],
p4 = 35159 [Type: Int32 (0)]
代码中没有其他任何内容发生变化——只有配置文件。
有谁知道这里出了什么问题?我怀疑它要么是使用序列来生成表(STAT_SEQ
列)的 ID,要么是导致第二组 SQL 中的参数为?
而不是p0
等的任何原因p1
。