从xlsx文件获取数据并插入oledb连接。 我想通过添加线程或多线程(如果可能)来使其更快。这是我的代码...任何想法请我帮忙
Public Sub readEEdata()
Dim eedatapath As String = MainForm.TxtEEData.Text
Dim tempinfo As New infocls
Dim fi As IO.FileInfo = New IO.FileInfo(MainForm.TxtEEData.Text)
Using excelPackage As New ExcelPackage(fi)
Dim firstWorksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets(1)
Dim colCount As Integer = firstWorksheet.Dimension.End.Column
Dim rowCount As Integer = firstWorksheet.Dimension.End.Row
For row As Integer = 2 To rowCount
With tempinfo
MainForm.LblStatus.Text = "Importing EE data: " & row & " " & GetValue(firstWorksheet, "A" & row.ToString)
.ID = GetValue(firstWorksheet, "A" & row.ToString)
.Fname = GetValue(firstWorksheet, "D" & row.ToString)
.lname = GetValue(firstWorksheet, "B" & row.ToString)
.mname = GetValue(firstWorksheet, "E" & row.ToString)
.tinum = GetValue(firstWorksheet, "F" & row.ToString)
If .Fname <> Nothing AndAlso .Fname.Contains("'") Then .Fname = .Fname.Replace("'", "´")
If .lname <> Nothing AndAlso .lname.Contains("'") Then .lname = .Fname.Replace("'", "´")
If .mname <> Nothing AndAlso .mname.Contains("'") Then .mname = .Fname.Replace("'", "´")
End With
If tempinfo.ID <> Nothing And tempinfo.Fname <> Nothing Then
saveEEData(tempinfo)
End If
Next
End Using
Public Sub saveEEData(ByVal infoclass As infocls)
masterConnection = New OleDb.OleDbConnection(connString)
masterConnection.Open()
masterCommand.Connection = masterConnection
masterCommand.CommandText = "Insert into EEData Values('" & infoclass.ID & "', '" & infoclass.lname & "', '" & infoclass.Fname & "','" & infoclass.mname & "','" & infoclass.tinum & "')"
masterCommand.ExecuteNonQuery()
masterConnection.Close()
End Sub
答案 0 :(得分:0)
我不知道您在用那个标签做什么,但是它只会在您的循环中不断被覆盖。您唯一会看到的是最后一次迭代。
无需将数据分配给类的属性。只需将它们直接分配给参数的值即可。
使用...结束使用块可确保关闭并处置您的数据库对象,即使发生错误也是如此。始终在sql语句中使用参数。您将需要在数据库中检入正确的数据类型和字段大小。这些参数在循环外添加一次,并且只有值在循环内更改。
使用参数时,您不必担心名称中的单引号。
这可能会加快速度,因为您已打开和关闭连接10,000次!啊!
Public Sub readEEdata()
Dim eedatapath As String = MainForm.TxtEEData.Text
'Don't access the text box a second time, you already have the value
Dim fi As IO.FileInfo = New IO.FileInfo(eedatapath)
Using excelPackage As New ExcelPackage(fi)
Try
Dim firstWorksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets(1)
Dim colCount As Integer = firstWorksheet.Dimension.End.Column
Dim rowCount As Integer = firstWorksheet.Dimension.End.Row
Using cn As New OleDbConnection(connString),
cmd As New OleDbCommand("Insert into EEData Values(@ID, @Lname, @Fname,@Mname,@tinum);", cn)
With cmd.Parameters
.Add("@ID", OleDbType.Integer)
.Add("@Lname", OleDbType.VarChar, 100)
.Add("@Fname", OleDbType.VarChar, 100)
.Add("@Mname", OleDbType.VarChar, 100)
.Add("@tinum", OleDbType.VarChar, 100)
End With
cn.Open()
For row = 2 To rowCount
cmd.Parameters("@ID").Value = GetValue(firstWorksheet, "A" & row.ToString)
cmd.Parameters("@Lname").Value = GetValue(firstWorksheet, "B" & row.ToString)
cmd.Parameters("@Fname").Value = GetValue(firstWorksheet, "D" & row.ToString)
cmd.Parameters("@Mname").Value = GetValue(firstWorksheet, "E" & row.ToString)
cmd.Parameters("@tinum").Value = GetValue(firstWorksheet, "F" & row.ToString)
cmd.ExecuteNonQuery()
Next
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Using
End Sub
另一种方法是创建并填充数据表。然后使用DataAdapter一次完成所有更新。