Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
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

VB
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



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.
Posted
Updated 28-Jun-11 5:56am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jun-11 11:47am    
VB or VB.NET? If both, why? Please tag properly.
--SA
william_nhl 28-Jun-11 11:57am    
I need it in vb.

1 solution

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[^]
 
Share this answer
 
Comments
william_nhl 28-Jun-11 12:25pm    
ok, but how can i modify the loading csv source code above to made it auto load all the csv file in the folder into the dgv? Can it be done?
Mehul M Thakkar 29-Jun-11 0:55am    
Yes, it can be done. Fill your dataset with all your CSV file in different datatable, I believe the structure of your CSV files are same. Once your dataset is ready with all the datatables, just merge all the datatables in a single datatable and bind to dgv. Along with your CSV file data, you can create extra column datatable with your file name, which you should not display in grid. Use that column value to store in your db.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900