我编写了一些代码,用于将数据从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数据库,但我收到此错误:
答案 0 :(得分:1)
很高兴看到您至少尝试使用参数。我要做的第一件事就是在您的参数名称前加上@
:
Dim dataid As New OleDbParameter With {.DbType = DbType.Int32, .ParameterName = "@dataid"}
接下来我注意到您尚未将参数添加到OleDbCommand
:
cmd.Parameters.Add(dataid)
最后,Insert
语句看起来不太合适。您应该在表中指定列名以及要插入的值。同时将VALUE
更改为VALUES
:
"INSERT INTO TblProject (field1, field2, field3...) VALUES (@dataid, @name ,@company...)"
您可以将field1
,field2
,field3
更改为正确的列名称,当然也要确保已指定所有列名称。
对于OleDbCommand
,重要的是要注意它不是参数的名称,而是声明它们的重要顺序。
接下来我还会考虑实施Using:
有时,您的代码需要非托管资源,例如文件句柄,COM包装器或SQL连接。使用块可确保在代码完成后处理一个或多个此类资源。这使得它们可供其他代码使用。
以下是一些示例代码。请注意,我使用?
作为参数占位符:
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
正如我所说,它只是示例代码,所以你必须用其余的字段填充空白参数,但这应该会让你重新开始。