从.net连接到oracle数据库时出错

时间:2012-09-17 12:49:38

标签: c# .net oracle10g

我正在使用System.data.oracleclient命名空间并将该dll放在我的项目

的引用中

我使用连接字符串

SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxx.xx.x.xxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xxxx)));uid=xxxx;pwd=xxxx;

在运行应用程序时,我收到响应消息

OCIEnvCreate failed with return code -1 but error message text was not available.

我不知道我被困在哪里......

任何人都可以为我提供任何指导方针来解决这个问题。

我太傻了但没有得到正确的解决方案。

等待您宝贵的建议和意见

更新1:

我使用的代码是

   con = new OracleConnection(conStr);
                con.Open(); // connection must be openned for command
                query = "insert into table1 values ('ss');
                cmd = new OracleCommand(query, con);
               if(cmd.ExecuteNonQuery()>0)
                {
                    Console.WriteLine("insert sucess!");
                }
                con.Close();

我的堆栈跟踪错误是

   at System.Data.OracleClient.OciHandle..ctor(OciHandle parentHandle, HTYPE handleType, MODE ocimode, HANDLEFLAG handleflags)
   at System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String userName, String password, String serverName, Boolean integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
   at System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString connectionOptions)
   at System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.OracleClient.OracleConnection.Open()
   at oracleTest.Program.getconnection() in D:\oracle\oracletst[via-oracleclient]\oracletst\Program.cs:line 29
   at oracleTest.Program.Main(String[] args) in D:\oracle\oracletst[via-oracleclient]\oracletst\Program.cs:line 21
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

1 个答案:

答案 0 :(得分:0)

我看到的两件事可能会导致问题

  1. 您没有处置ado.net对象
  2. con& cmd似乎是共享变量。
  3. 试试这个并查看问题是否仍然存在。

    using(var con = new OracleConnection(conStr))
    using(var cmd = con.CreateCommand())
    {
        con.Open(); 
        cmd.CommandText = "insert into table1 values ('ss');
        if(cmd.ExecuteNonQuery() > 0)
        {
            Console.WriteLine("insert sucess!");
        }
    }
    

    因为我们使用的是using关键字,所以当退出该块时,它会自动调用con / cmd.Dispose()。 Dispose()将自动调用Close()。

    修改 确保您的连接字符串格式正确。 http://www.connectionstrings.com/oracle