OleDBDataReader:没有给出一个或多个必需参数的值

时间:2016-10-31 11:36:20

标签: .net vb.net oledbconnection oledbdatareader

在以下脚本中,我将.xls转换为.csv文件。 这项工作到目前为止:

Const cDelimiter As Char = "~"c 'Delimiter for the new CSV File
    Const fileHasHeader As String = "YES"

    Dim sSheetName As String = String.Empty
    Dim sConnection As String =
    String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR={1};IMEX=1""", fullPathSource, fileHasHeader)

    '
    Try
        Using oleExcelConnection As New OleDb.OleDbConnection(sConnection)
            oleExcelConnection.Open()
            Using dtTablesList = oleExcelConnection.GetSchema("Tables")

                If dtTablesList.Rows.Count > 0 Then
                    sSheetName = dtTablesList.Rows(0)("TABLE_NAME").ToString
                End If
                ' Check if the data sheet has a worksheet(table)
                If sSheetName <> "" Then

                    Dim oleExcelCommand As OleDb.OleDbCommand = oleExcelConnection.CreateCommand()
                    oleExcelCommand.CommandText = "Select * From [" & sSheetName & "]"
                    oleExcelCommand.CommandType = CommandType.Text
                    Dim oleExcelReader As OleDb.OleDbDataReader = oleExcelCommand.ExecuteReader

                    Dim dtCols As DataTable = oleExcelConnection.GetOleDbSchemaTable()
                    For Each elem In dtCols.Columns
                        Dim string123 As String = (elem.ToString)

                    Next

                    'Dim command As New OleDb.OleDbCommand(insertSQL)
                    'Date.Now.ToString().Replace(":", "-").Replace(".", "-"))
                    Using objStreamWriter As New IO.StreamWriter(String.Format("{0}.csv", fullPathTarget))

                        'Row Count not in use so far
                        Dim countRow As Integer = 0
                        Dim strToRow As String = Nothing

                        'Read the XLS(x) file until end
                        While oleExcelReader.Read()
                            'Empty String for next run
                            Dim rowString As String = ""
                            'Check dynamically for length of rows
                            For i As Integer = 0 To oleExcelReader.FieldCount - 1 Step 1
                                'Here would be the place tó remove any unwanted delimiters within the data
                                'If oleExcelReader.GetValue(i).ToString.Contains(",") Then

                                'End If

                                'Append String to write end the end of each row
                                rowString = rowString & oleExcelReader.GetValue(i).ToString() & cDelimiter
                            Next
                            'Write data to file
                            objStreamWriter.WriteLine(rowString)
                            countRow += countRow
                        End While
                    End Using
                    'End using will close all open connections
                End If
            End Using
            'End using will close all open connections
        End Using
        'End using will close all open connections

现在我想添加一个过滤器来删除空行: 所以我替换了这一行

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "]"

有了(也在工作):

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> ''"

另一种变化(也在起作用):

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> '' AND [USERNAME] <> ''"

但这会崩溃:

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE [APPLICATION] <> '' OR [USERNAME] <> ''"

为什么我不能在这个地方使用OR。那不是普通的SQL吗?

我会在线上获得例外(&#34;没有给出一个或多个必需参数的值#34;)。

Dim oleExcelReader As OleDb.OleDbDataReader = oleExcelCommand.ExecuteReader

1 个答案:

答案 0 :(得分:1)

这看似微不足道,但您尝试过使用括号

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE ([APPLICATION] <> '') OR ([USERNAME] <> '')"

此外,通过反向逻辑,您将获得与AND

等效的查询
oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE NOT ([APPLICATION] = '' AND [USERNAME] = '')"