2016-10-31 4 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 & "] WHERE [APPLICATION] <> ''" 
: 그래서 나는이 선 (또한 작업) 그와

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

교체

및 다른 변형 (작동 중) :

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

은 그냥 사용할 수 있습니까 ... [USERNAME] <> " '"'? – topshot

+0

@topshot 예, 신호 단어 인 것 같습니다 ... 나는 이해하지 못합니다 ... – Ulpin

답변

1

그것은 사소한 보일 수 있습니다,하지만 당신은 당신이 AND에 해당하는 쿼리를 얻을 것 반대 논리에 의해,

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

또한 괄호를 사용하여 시도했다 :`

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

아니요이 또한 작동하지 않습니다. 우리는 구문을 사용하지만 아무도 작동하지 않습니다. – Ulpin

+0

반전 논리에 관한 귀하의 의견은 좋은 생각이지만 실생활에서는 좀 더 복잡하고이 조건에서 OR을 대체하는 방법을 모르겠습니다. A & (B Or C) – Ulpin

+0

WHERE ([ APPLICATION = 'AND ([하지 USERNAME] ='여부 [AUTORIZATION) = '')) – Ulpin