繁体   English   中英

连接到Oracle数据库时出错(ORA-12560)

[英]Error connecting to Oracle database (ORA-12560)

尝试连接到我正在开发的VB.net应用程序中的数据库时,遇到标题中提到的错误。 我知道这不是侦听器或任何服务问题,因为我能够在开发的另一个应用程序中以相同的凭据连接到相同的数据库(我无法与正在开发的应用程序连接后运行该应用程序,因此它不是Windows事件日志)。

两者都使用Oracle.DataAccess.Client.OracleConnection引用现有的ODBC连接。 创建和打开连接的方式略有不同,因此我认为是导致问题的VB代码所致,但是我一生都无法想到。

非功能代码:

Public Function EstablishCon(ByVal odbc As String, 
        ByVal uname As String, 
        ByVal pass As String) As Oracle.DataAccess.Client.OracleConnection
    '
    Dim constr As String
    Dim scon As Oracle.DataAccess.Client.OracleConnection = Nothing

    Try
        constr = "Data Source=" & odbc & ";User Id=" & uname & ";Password=" & pass & ";"
        scon = New Oracle.DataAccess.Client.OracleConnection(constr)
        scon.Open()
    Catch ex As Exception
        MsgBox("Encountered an error while creating the Oracle connection string and opening the connection. This will cause the application to be unable to access any data." _
            & " As such the application will close following the closure of this error message." & Chr(10) & Chr(10) & "Error Details: " & ex.Message, vbOKOnly, _
            "Critical Error: Failed to the Oracle Database")
        scon.ConnectionString = Nothing
    End Try

    Return scon
End Function

而我的工作代码如下所示:

Private Sub MainWin_Shown(sender As Object, e As EventArgs) Handles Me.Shown
    '
    'Dim scmd As New SqlClient.SqlCommand
    Dim QTxt As String = ""
    Dim ConStr As String = "Data Source=existing ODBC connection name;User Id=user_name;Password=pass;"
    Dim scon As New Oracle.DataAccess.Client.OracleConnection(ConStr)
    Dim d As New DataStore
    Dim scmd As New Oracle.DataAccess.Client.OracleCommand
    Dim odr As Oracle.DataAccess.Client.OracleDataReader

    'Setup the datatable in d
    Try
        d.DT.Columns.Add("App_Type")
        d.DT.Columns("App_Type").DataType = GetType(String)
        d.DT.Columns.Add("CPU_Seconds")
        d.DT.Columns("CPU_Seconds").DataType = GetType(Double)
        'd.DT.Columns.Add("Pct_Of_CPU")
        'd.DT.Columns("Pct_Of_CPU").DataType = GetType(Double)
        d.DT.Columns.Add("RunDate")
        d.DT.Columns("RunDate").DataType = GetType(Date)
    Catch ex As Exception
        Me.Errors.Text = "Encountered an error setting up the data table that will receive the query data. Details: " & ex.Message
        d = Nothing
        scon = Nothing
        scmd = Nothing
        ConStr = Nothing
        QTxt = Nothing
        odr = Nothing
        Exit Sub
    End Try


    Me.Status.Text = Now() & " - Building the SQL executor"
    Me.Refresh()
    'Build the query executor
    Try
        scmd.CommandType = CommandType.Text
        scmd.Connection = scon

        'Capture the query text
        QTxt = 'Some text that makes a valid query 

        scmd.CommandText = QTxt
    Catch ex As Exception
        Me.Errors.Text = "An error occurred while building the SQL Executor. Details: " & ex.Message & Chr(10) & Chr(10) & Me.Errors.Text
        d = Nothing
        scon = Nothing
        scmd = Nothing
        ConStr = Nothing
        QTxt = Nothing
        odr = Nothing
        Exit Sub
    End Try

    Me.ProgBar.Step = 5
    Me.ProgBar.PerformStep()
    Me.Status.Text = Now() & " - Connecting to the database" & Chr(10) & Me.Status.Text
    Me.Refresh()
    Try
        'Open the connection
        scon.Open()
    Catch ex As Exception
        Me.Errors.Text = "An error occurred while opening the SQL connection. Details: " & ex.Message & Chr(10) & Chr(10) & Me.Errors.Text
        d = Nothing
        scon.Close()
        scon = Nothing
        scmd = Nothing
        ConStr = Nothing
        QTxt = Nothing
        odr = Nothing
        Exit Sub
    End Try

   'Some more stuff that's not relevant
End Sub

抱歉,格式混乱,我花了15分钟试图将两个代码块分开,但是StackOverflow却没有。

因此,两者之间的唯一真正区别是,一个在声明Connection变量时填充了连接字符串,另一个在以后的一个单独函数中填充了连接字符串。 由于错误在函数内部引发并被捕获,因此函数方面似乎没有考虑在内。 一世

所以问题是我如何调用调用该函数的表单。 我曾经使用.Show而不是.ShowDialog

这导致在点击Shown事件(调用连接函数)之前清除了表单变量(包含作为odbcuname和& pass传入的值)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM