首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

csv格式文件导入数据库,解决办法

2012-02-28 
csv格式文件导入数据库,急急急急vb.net代码实现将csv格式数据导入到access数据库是怎么实现的 ,怎么样获取

csv格式文件导入数据库,急急急急
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) + ")"


cmd = New MySqlCommand(strSqlStatement, conn)
iReturn = cmd.ExecuteNonQuery()
End If

Catch ex As Exception
Throw ex

Finally
conn.Close()
conn.Dispose()
End Try

End If

strLine = sr.ReadLine()

End While

Catch ex As Exception

Finally
sr.Close()
End Try

热点排行