从FbDataAdapter

时间:2017-01-23 17:34:15

标签: vb.net ado.net firebird dataadapter

我的商务网站上有几个免费提供的数据访问层。我一直在他们的网站上关注Firebird数据库,现在决定为这个数据库引擎构建一个新的数据访问层。

我正在使用Firebird网站(https://www.firebirdsql.org/en/net-provider/)上推荐的ADO.NET提供程序。使用的版本是5.5。

我开发了一个简单的存储过程来处理返回的几列数据,这些数据已在我的数据库管理器中成功测试过。它也已成功通过我为Firebird数据访问层构建的测试客户端中的数据读取器进行了测试。

但是,当我尝试针对返回数据集测试存储过程时,我一直收到以下错误...

  

无法转换类型为' System.String'的对象输入' System.Byte []'

即使是带参数的简单内联查询也会产生相同的错误。

然而,这些完全相同的测试与我的数据阅读器流程完美配合。

当涉及到我的代码中的#34;填充"时,总会产生这个错误。来自Firebird数据适配器的数据集。

我正在测试的代码在

之下
Public Overloads Function ExecuteDataSet(ByVal psConnectionString As String, _
                                         ByVal psQuery As String, _
                                         ByRef poParameterStructures As ArrayList) As DataSet

    Dim loParameterStructures  As New ArrayList()

    Dim loDataSet                As DataSet
    Dim loFbSqlConnection        As FbConnection = Nothing
    Dim loFbSqlParameter         As FbParameter
    Dim loFbSqlCommand           As FbCommand = Nothing
    Dim loFbSqlDataAdapter       As FbDataAdapter = Nothing


    Try 
        'DataTable dt = new DataTable();
        'FbDataAdapter da = new FbDataAdapter("select_mytable", ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);
        'da.SelectCommand.CommandType = CommandType.StoredProcedure;
        'da.SelectCommand.Parameters.Add("@id", 123);
        'da.Fill(dt);

        loFbSqlConnection = Get_DBConnection(psConnectionString)

            loFbSqlCommand = New FbCommand(psQuery, loFbSqlConnection)
            loFbSqlCommand.CommandTimeout = ciTimeOut

            ' check for parameterized inline SQL or stored-procedure
            If ((psQuery.IndexOf("@") > -1) Or _
                (psQuery.ToUpper().IndexOf("SELECT ") > -1)) Then
                loFbSqlCommand.CommandType = CommandType.Text
            Else        
                loFbSqlCommand.CommandType = CommandType.StoredProcedure      
            End If  


            ' create parameters for stored-procedure
            If (Not poParameterStructures Is Nothing) Then
                For Each loFbSqlHelperParameterStructure As FbSqlHelperParameterStructure In poParameterStructures
                    loFbSqlParameter = New FbParameter() 

                        loFbSqlParameter.ParameterName = loFbSqlHelperParameterStructure.PARAMETER_NAME
                        loFbSqlParameter.DbType = loFbSqlHelperParameterStructure.PARAMETER_FBSQLDBTYPE
                        loFbSqlParameter.Direction = loFbSqlHelperParameterStructure.PARAMETER_DIRECTION

                        Select Case loFbSqlParameter.Direction  
                            Case ParameterDirection.Input:
                                loFbSqlParameter.Value = loFbSqlHelperParameterStructure.PARAMETER_VALUE
                                loFbSqlParameter.Size = loFbSqlHelperParameterStructure.PARAMETER_SIZE

                            Case ParameterDirection.Output:
                                loFbSqlParameter.Size = loFbSqlHelperParameterStructure.PARAMETER_SIZE
                        End Select

                    loFbSqlCommand.Parameters.Add(loFbSqlParameter)
                Next
            End If

        loFbSqlDataAdapter = New FbDataAdapter()                
        loFbSqlDataAdapter.SelectCommand = loFbSqlCommand

        loDataSet = New DataSet()
        loFbSqlDataAdapter.Fill(loDataSet)
    Catch loFbSqlException As FbException
        Throw loFbSqlException
    Catch loException As Exception
        Throw loException
    Finally
        loFbSqlCommand.Dispose()
        loFbSqlConnection.Close()
        loFbSqlConnection.Dispose()
    End Try


    poParameterStructures = loParameterStructures

    Return (loDataSet)
End Function

上面的代码也已成功用于支持SQL Server,SQL Server CE,MySQL和PostgreSQL的其他数据访问层。

但是,对于Firebird数据库,无论从Firebird数据库中获取数据集的所有文档中的代码设置如何,都会产生与上述完全相同的错误。因此,此处提供的代码是在我的其他数据访问层中成功使用的原始代码。

1 个答案:

答案 0 :(得分:0)

我终于找到了上面代码的问题......

如果查看我的“Finally”语句,您将看到以下代码行...“loFbSqlCommand.Dispose()”

尽管如此,这行代码已在我分发的其他数据访问层中成功使用,但对于Firebird ADO.NET数据提供程序,它会将一系列引用指针分解为返回的数据集,这是一个完全独立的对象。 FbDataAdapter。

删除这行代码或将其注释掉,代码工作正常。