2016-10-31 38 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

相關問題