csv格式文件导入数据库,解决办法
csv格式文件导入数据库,急急急急
vb.net代码实现将csv格式数据导入到access数据库是怎么实现的 ,怎么样获取导入的数据?
------解决方案--------------------
vb.net代码实现将csv格式数据导入到access数据库是怎么实现的 ,怎么样获取导入的数据?
------解决方案--------------------
- VB.NET code
Private Sub Botton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click xlsName = My.Application.Info.DirectoryPath & "\temp.xls" '用 Add方法 创建新的工作簿和工作表对象。 xlApp = New Microsoft.Office.Interop.Excel.Application xlBook = xlApp.Workbooks.Open(xlsName) 'Set xlBook = xlApp.Workbooks.Add xlSheet = xlBook.Worksheets("work") xlSheet.Name = "work" ' If Err.Number = 1004 Then ' xlSheet.Name = formatdatetime(DateTimePicker1, "yyyymm") ' MsgBox (DateTimePicker1 & "工作表已存在将自动产生sheel系列工作表") ' Err.Number = 0 ' End If '将data3的值赋给Microsoft Excel 单元。 'sqlString = "select * from worktable" 'If Conn.State = 1 Then ' If Rs.State = 1 Then Rs.Close() ' Rs.Open(sqlString, Conn, 1, 1) 'Else ' Conn.Open(conString) ' Rs.Open(sqlString, Conn, 1, 1) 'End If For i = 0 To 5 For j = 0 To 7 xlSheet.Cells._Default(i + 1, j + 1).Value = i & "," & j Next j Next i 'xlSheet.Cells(3, 1).Formula = "=R1C1 + R2C1" '保存工作表 'xlSheet.StandardWidth = 2 'xlSheet.SaveAs (App.path & "\" & formatdatetime(DateTimePicker1, "yyyy年mm月值班表") & formatdatetime(Now(), "hhmmss") & ".xls") xlBook.Save() xlBook.Close() '用 Quit 方法关闭 Microsoft Excel xlApp.Quit() '释放对象 Dim pro As Process For Each pro In Process.GetProcesses If pro.ProcessName = "EXCEL" Then pro.Kill() End If Next End Sub
------解决方案--------------------
Try
'sw = File.CreateText(strLogFileName)
While strLine <> ""
Dim aryline() As String
Dim aryline1() As String
'一般csv文件默认是 按逗号分割,这个事例中是按照 | 分割
aryline = strLine.Split("|")
aryline1 = strLine1.Split(",")
If iColumsCount = aryline.Length Then
Dim i As Int16 = 0
Dim item As String = String.Empty
strValues = String.Empty
For Each item In aryline
strValues += "" + aryline1(i) + "" + "=" + "'" + item.Trim().Replace(",", "") + "'" + ","
i = i + 1
Next
Dim strSqlStatement As String = strSqlUpdCmd + "" + strValues.Substring(0, strValues.Length - 1) + " WHERE " + aryline1(12) + " = '" + aryline(12).Trim() + "' LIMIT 1;"
'db operation
Try
conn = New MySqlConnection(strConnSql)
conn.Open()
cmd = New MySqlCommand(strSqlStatement, conn)
iReturn = cmd.ExecuteNonQuery()
If iReturn = 0 Then
strValues1 = String.Empty
For Each item In aryline
strValues1 += "'" + item.Trim() + "'" + ","
Next
strSqlStatement = String.Empty
strSqlStatement = strSqlInsertCmd + "(" + strValues1.Substring(0, strValues1.Length - 1) + ")"