Import csv into DataTableDataGridView and auto save to SQL CE database

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am new to programming and need some guidance.
I am using Visual Basic Express 2010. I have created 2 Window Forms and I have created the following:
  • A local SQL 3.5 CE Database named (Database1.sdf)
  • A DataSet named (Database1DataSet.xsd)
  • A DataTable named (DataTable1)
I have linked Form2 to the Data Source by dragged DataTable1 onto form2. As a result, Visual Basic Express created the following:
  • [background=#fafafa]DataTableBindingSource[/background]
  • [background=#fafafa]DataTableAdapter[/background]
  • [background=#fafafa]DataTableBindingNavigator[/background]
  • [background=#fafafa]TableAdapterManager. [/background]
  • [background=#fafafa]A toolstrip of form controls (save, delete, etc.)[/background]
When I run the program, I can type values into the table and hit the save button that visual express posted. The entered values save and are seen when the form is re-opened.
So, I would like to do the following.
  • On Form1: Button1 opens file dialog. User selects the csv file. Then Form2 opens to show the imported csv data in the Data Table and auto saves the data to the database.
My Button1 on Form1 Code Follows:
Public Class Form1

Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:"
openFileDialog1.Filter = "txt files (*.csv)|*.csv|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 1
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then

Dim frm2 As New Form2(openFileDialog1.FileName)
frm2.Show()

End If
End Sub
Form 2 has the following so far:
Imports System.IO
Imports System.Data.OleDb
Imports System.Windows.Forms.DataGridView

Public Class Form2

Public Sub New(ByVal fileName As String)

InitializeComponent()

Dim file As String = IO.Path.GetFileName(fileName)
Dim path As String = IO.Directory.GetParent(fileName).FullName
Dim dt As New DataTable

Try

If IO.File.Exists(IO.Path.Combine(path, file)) Then
Dim ConStr As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
path & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""
Dim conn As New OleDb.OleDbConnection(ConStr)
Dim da As New OleDb.OleDbDataAdapter("Select * from " & _
file, conn)
da.Fill(dt)

End If

Catch ex As Exception
MessageBox.Show(ex.ToString)

End Try

DataTable1DataGridView.DataSource = dt

End Sub
End Class


So, what do I need to do to form2 to have the DataTable1DataGridView populate with the csv data and auto save to the established Database. Please note SQL bulk insert does not work on SQL CE.
Thanks in Advance!

View the full article
 
Back
Top