我尝试连接到我正在开发的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变量时填充连接字符串,而另一个在稍后的单独函数中填充。功能方面似乎不会因为错误被抛出并被捕获到函数内部。我
答案 0 :(得分:0)
所以问题是我如何调用调用该函数的表单。我使用.Show
而不是.ShowDialog
。
这导致表单变量(保存在[{1}},odbc
和& uname
中传递的值)在点击pass
之前被清除event(调用连接函数的地方)。