继续将其他CSV文件加载到数据网格视图中 [英] Continue load different CSV file into data grid view

查看:89
本文介绍了继续将其他CSV文件加载到数据网格视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim objAdapter1 As New OleDbDataAdapter
        Dim objDataset1 As New DataSet
        Dim row As Integer = 0
        objAdapter1.Dispose()
        objDataset1.Clear()
        dgv1.DataSource = Nothing
        dgv1.Refresh()
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Try
                Dim a As New FileInfo(OpenFileDialog1.FileName)
                Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Text;Data Source=" & a.DirectoryName
                Dim objConn As New OleDbConnection(sConnectionString)
                objConn.Open()
                Dim objCmdSelect As New OleDbCommand("SELECT Name , Bytes , Packets , BitsPerSecond , PacketsPerSecond ,BytesPercentage , PacketsPercentage   FROM " & a.Name, objConn)

                objAdapter1.SelectCommand = objCmdSelect

                objAdapter1.Fill(objDataset1, "test")
                dgv1.DataSource = objDataset1.Tables(0).DefaultView
                objConn.Close()
                'End If

            Catch ex As Exception

            End Try


Try

                dgv1.AllowUserToAddRows = False

                For Each DataRow As DataGridViewRow In dgv1.Rows    'save data to Protocol table
                    Dim z As String = dgv1.Rows(row).Cells("Name").Value
                    Dim b As String = dgv1.Rows(row).Cells("Bytes").Value
                    Dim c As String = dgv1.Rows(row).Cells("Packets").Value
                    Dim d As String = dgv1.Rows(row).Cells("BitsPerSecond").Value
                    Dim y As String = dgv1.Rows(row).Cells("PacketsPerSecond").Value
                    Dim f As String = dgv1.Rows(row).Cells("BytesPercentage").Value
                    Dim g As String = dgv1.Rows(row).Cells("PacketsPercentage").Value

                    connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\AnalyzerDb.mdb")
                    connection.Open()
                    cmd = New OleDbCommand("INSERT INTO ProtocolExistUsed VALUES('" & z & "','" & b & "','" & c & "','" & d & "','" & y & "','" & f & "','" & g & "')", connection)
                    cmd.ExecuteNonQuery()
                    row += 1
                    connection.Close()

                Next
            Catch
                MsgBox(ErrorToString, vbCritical, "Search")
            End Try





        Else
            MsgBox("Please insert Data. ", MsgBoxStyle.Information, "error")    'for non exist data in csv file
        End If




上面是将csv文件加载到数据网格视图,然后将其保存到Access 2003数据库中的源代码.但是此代码只能让用户每次将1个文件加载到dgv中并保存.所以我的问题是我可以让用户juz按下1个按钮,然后系统将根据Filename或File ID将所有文件加载到dgv中并保存到数据库中.谢谢.




The above is a source code to load a csv file to a data grid view then save it into the access 2003 database. But this code only can let the user each time to load 1 file into dgv and save it. So my problem is can i make it to let user juz hit 1 button then the the system will load all the file into the dgv and save into the database according to Filename or File ID. Please help me and show me how to make it.Thanks.

推荐答案

用相同的方法可以为数据集中的另一个csv文件创建另一个表,最后可以合并所有使用合并功能在单个数据表中存储表数据.请参考以下链接:

http://msdn.microsoft.com/en -us/library/system.data.datatable.merge(v = VS.90).aspx [
Same way you can create another table for another csv file in dataset and finally you can merge all the table data in a single datatable using merge function. Refer the following link:

http://msdn.microsoft.com/en-us/library/system.data.datatable.merge(v=VS.90).aspx[^]


这篇关于继续将其他CSV文件加载到数据网格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆