2017-08-01 104 views
0

我已經編寫了一些代碼來將數據從Excel文件導入到Access數據庫中。代碼首先選擇一個目錄,然後單擊一個按鈕導入。將Excel文件導入到Access數據庫中

這是我要導入到數據庫中的字段:

dataid - ID 

name - NAME 

company - COMPANY 

phone - PHONE NUMBER 

Add1 - ADDRESS 1 

Add2 - ADDRESS 2 

Add3 - ADDRESS 3 

pcode - POSCODE 

city - CITY NAME 

state - STATE 

remark - REMARK 

ShipperRef - REFFERENCE 

可能有多個行這是我無法預測。

代碼:

Private Sub TransferExcelFile2Database_Click(sender As System.Object, e As System.EventArgs) Handles TransferExcelFile2Database.Click 

      Try 

      Dim connectexcelstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Text1.Text & "';" & _ 
       "Extended Properties=""Excel 8.0;HDR=yes""" 
      Dim con As OleDbConnection = New OleDbConnection(connectexcelstring) 

      con.Open() 

      Dim query As String = "Select * from [Sheet1$]" 

      Dim excelcmd As OleDbCommand = New OleDbCommand(query, con) 


      dt.Load(excelcmd.ExecuteReader()) 

      If dt.Rows.Count > 0 Then 
       Dim conn As OleDbConnection = New OleDbConnection(constring) 
       Dim cmdAccess As New OleDbCommand 

       Dim dataid As New OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "dataid"} 
       Dim name As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "name"} 
       Dim company As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "company"} 
       Dim phone As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "phone"} 
       Dim Add1 As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "Add1"} 
       Dim Add2 As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "Add2"} 
       Dim Add3 As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "Add3"} 
       Dim pcode As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "pcode"} 
       Dim City As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "City"} 
       Dim state As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "state"} 
       Dim remark As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "remark"} 
       Dim ShipperRef As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "ShipperRef"} 

       cmdAccess.CommandText = "INSERT INTO TblProject VALUE (@dataid,@name,@company,@phone,@Add1,@Add2,@Add3,@pcode,@City,@state,@remark,@ShipperRef)" 

       conn.Open() 

       Dim Col1Value As Integer 
       Dim Result As Integer 
       For Each row As DataRow In dt.Rows 
        If Integer.TryParse(row("dataid").ToString, Col1Value) Then 
         cmdAccess.Parameters("dataid").Value = row.Item("dataid") 
         cmdAccess.Parameters("name").Value = row.Item("name") 
         cmdAccess.Parameters("company").Value = row.Item("company") 
         cmdAccess.Parameters("phone").Value = row.Item("phone") 
         cmdAccess.Parameters("Add1").Value = row.Item("Add1") 
         cmdAccess.Parameters("Add2").Value = row.Item("Add2") 
         cmdAccess.Parameters("Add3").Value = row.Item("Add3") 
         cmdAccess.Parameters("pcode").Value = row.Item("pcode") 
         cmdAccess.Parameters("city").Value = row.Item("city") 
         cmdAccess.Parameters("state").Value = row.Item("state") 
         cmdAccess.Parameters("remark").Value = row.Item("remark") 
         cmdAccess.Parameters("ShipperRef").Value = row.Item("ShipperRef") 

         Result = cmdAccess.ExecuteNonQuery() 
         If Result = 0 Then 
          MsgBox("Mission to insert failed") 
         End If 
        Else 
         ' 
         ' Value does not represent a valid integer. 
         ' You need to decide how to handle this i.e. 
         ' Abort insert as done here or place a default value in. 
         ' 
        End If 
       Next 

      End If 


     Catch ex As Exception 

      MessageBox.Show(ex.Message) 
     End Try 

    End Sub 

我想在Excel中的數據導入到Access數據庫,但我收到此錯誤:

回答

1

很高興見到你至少嘗試使用參數。我會做的第一件事是在前面加上@到您的參數名稱:

Dim dataid As New OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "@dataid"} 

我注意到接下來的事情是,你還沒有加入參數,您OleDbCommand:最後的Insert聲明不

cmd.Parameters.Add(dataid) 

看起來很正確。您應該在表中指定列名以及要插入的值。也改變VALUEVALUES

"INSERT INTO TblProject (field1, field2, field3...) VALUES (@dataid, @name ,@company...)" 

你會改變field1field2field3到右列的名字,當然,確保所有的列名已被指定。

OleDbCommand重要的是要注意,它不是參數的名稱,而是它們聲明的順序很重要。

繼續上我也會考慮實施Using

Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.

下面是一些示例代碼。請注意,我用?作爲我的參數佔位符:

Using con As New OleDbConnection(connectexcelstring), 
     cmd As New OleDbCommand("INSERT INTO TblProject (field1, field2, field3...) VALUES (?, ?, ?...)", con) 

    'You could also add the parameters to the command at this point 
    cmd.Parameters.Add("@dataid", OleDbType.Integer) 
    cmd.Parameters.Add("@name", OleDbType.VarChar) 
    ... 

    For Each row As DataRow In dt.Rows 
     Dim Col1Value As Integer 
     If Integer.TryParse(row("dataid").ToString(), Col1Value) Then 
      cmd.Parameters("@dataid").Value = Col1Value 'If Integer.TryParse is successful then "Col1Value" will also contain the value of "row("dataid")" 
      cmd.Parameters("@name").Value = row.Item("name").ToString() 
      ... 

      If cmd.ExecuteNonQuery() = 0 Then 
       MessageBox.Show("Mission to insert failed") 
      End If 

     Else 
      ' Value does not represent a valid integer. 
      ' You need to decide how to handle this i.e. 
      ' Abort insert as done here or place a default value in. 
     End If 

    Next 

End Using 

就像我說的,這只是示例代碼,所以你必須填寫你的領域,其餘參數的空白,但是這應該讓你回到你的辦法。

相關問題