我从oracle迁移到sqlserver。 因此,从OracleDataReader更改为OdbcDataReader。
我想使用GetName方法获取列的名称。(使用mysub子例程。)
但是,使用getName方法发生在ODBCException中。
错误[HY007] [Microsoft] [ODBC SQL Server驱动程序]未准备关联语句
我认为如果OdbcDataReader作为参数传递会发生,但即使写在main函数中它也会以相同的方式发生。
如果编写如下,一个getName方法引发异常,另一个可以正确获取列名。
在这个例子中它是简单的SQL,但实际上有各种SQL设置。
但是,由于所有SQL都没有转义,我认为不需要准备好的语句。
它在Oracle中正常运行。为什么会这样?
请告诉我如何解决。
Imports System.Data.Odbc
Module Module1
Sub Main()
Dim connection As OdbcConnection = Nothing
Try
'Set Connection String.
Dim constr As String = "Provider=MSDASQL; DSN=myDSN; UID=myDBUSR; PWD=myPass;"
connection = New OdbcConnection(constr)
'DB Open.
connection.Open()
'Begin Transaction
Dim tran As OdbcTransaction = connection.BeginTransaction
'Set SQL Command
Dim command As OdbcCommand = connection.CreateCommand()
command.CommandText = "SELECT * FROM [dbo].[TBL01]"
command.CommandTimeout = 1200
command.Transaction = tran
'ExecuteSQL
Dim reader As OdbcDataReader = command.ExecuteReader()
'Get column names with other functions.
mySub(reader)
'Get data with the main function
Dim colName As String = reader.GetName(0) ' -> Exception Occurd.
'ERROR [HY007] [Microsoft][ODBC SQL Server Driver]Associated statement is not prepared
'some logics...
tran.Commit()
Catch ex As Exception
Trace.TraceInformation(ex.Message & vbCrLf)
Trace.TraceInformation(ex.StackTrace)
Finally
connection.Close()
End Try
End Sub
Private Sub mySub(ByVal reader As OdbcDataReader)
Dim colName As String = reader.GetName(0) ' -> Exception Occurd.
'ERROR [HY007] [Microsoft][ODBC SQL Server Driver]Associated statement is not prepared
Trace.TraceInformation(colName)
End Sub
End Module