2016-10-31 48 views
0

在以下脚本中,我将.xls转换为.csv文件。 到目前为止是这种情况:OleDBDataReader:未给出一个或多个所需参数的值

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] <> ''" 

为什么我不能在这个whereclause使用OR。这不是简单的SQL?

我会在线上得到异常(“没有给出一个或多个必需参数的值”)。

Dim oleExcelReader As OleDb.OleDbDataReader = oleExcelCommand.ExecuteReader 
+0

是否与刚工作' ... WHERE [USERNAME] <>''“'? – topshot

+0

@topshot是的,它似乎是信号词OR。我不明白... – Ulpin

回答

1

这似乎微不足道,但你尝试用括号

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

此外,通过反向逻辑,你会得到相当的查询与AND

oleExcelCommand.CommandText = "Select * From [" & sSheetName & "] WHERE NOT ([APPLICATION] = '' AND [USERNAME] = '')" 
+0

没有这也是行不通的。我尝试了很多vario我们的语法,但没有任何工作。 – Ulpin

+0

您对反逻辑的评论是一个好主意,但我在现实生活中稍微复杂一点,我不知道如何在这种情况下替换OR: A&(B或C) – Ulpin

+0

WHERE(not [应用] = '' AND(未[USERNAME] = '' OR不[AUTORIZATION])= '')) – Ulpin

相关问题