我正在搜索为什么从excel到Sql Table的输入效果不佳(通过vb.net)
输入中的excel文件包含1300行和12列。最后五列包含价格。 输入后,我的表格中充满了excel数据,除了最后两个“价格”列,其中所有数据都是“空”。 奇怪的是,即使在excel文件中,sql目标表中的所有“价格”列都使用相同的类型(十进制(18,2))进行格式化。 对于副本,我使用SqlBulkCopy,我想知道这个功能是否不受限制。
这是我的代码:
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & fTransp & ";Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim RequeteExcelTransp As String = "SELECT * FROM [ExcelCetup$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(RequeteExcelTransp, ExcelConnection)
Dim objDR As OleDbDataReader
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(ConnexionLinkyStat)
bulkCopy.DestinationTableName = "dbo." & TableTransp & ""
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.WriteToServer(objDR)
objDR.Close()
ConnexionLinkyStat.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
ExcelConnection.Close()
答案 0 :(得分:0)
我正在尝试这个代码,我希望这段代码对您的项目有用
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim fBrowse As New OpenFileDialog
With fBrowse
.Filter = "Excel files(*.xlsx)|*.xlsx|All files (*.*)|*.*"
.FilterIndex = 1
.Title = "Import data from Excel file"
End With
If fBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim fname As String
fname = fBrowse.FileName
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source='" & fname & " '; " & "Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Test")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
MyConnection.Close()
For Each Drr As DataRow In DtSet.Tables(0).Rows
Execute_Local("INSERT INTO Excel(Name, Designation, Salary) VALUES ('" & Drr(0).ToString & "','" & Drr(1).ToString & "','" & Drr(2).ToString & "')")
Next
MsgBox("Successfully Saved")
End If
End Sub`