Solved

Inserting into one-to-many tables

Posted on 2004-10-05
3
206 Views
Last Modified: 2010-04-23
Hi Experts!!

How can i retrieve data from an excel sheet and insert them into 2 tables(access database) in into one-to-many  relation?

Very Needed!!!

thanks.
0
Comment
Question by:komlaaa
  • 2
3 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 12234676
U can retrieve the data from an excl worksheet on to a datatable . Eg: of getting it to a datatable is shown

 Private Sub btUpload_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btUpload.Click
        Dim objExcel As Excel.Application
        Dim objWorkBook As Excel.Workbook
        Dim objWorksheet As Excel.Worksheet
        Dim worklistsheet As Excel.Worksheet
        Dim dtUploadWorklist As New DataTable()
        Dim dtExcelTable As New DataTable("UploadTable")
        Dim excelRows As Int32
        Dim excelColumns As Int32
        Dim dgColumn As Integer
        Dim dgRow As Integer
        Dim i As Integer
        isExistingWorklistID = False

        dsUpload = New DataSet()
        'A table by name dtExcelTable is added to the dataset
        dsUpload.Tables.Add(dtExcelTable)
        Try
            'An excel object is being created.
            objExcel = CreateObject("Excel.Application")
            'If the user clicks on Ok in the OpenFile Dialog then, if the selected file
            'exists it is assigned to the excel object created.
            If (ofdWorklist.ShowDialog() = DialogResult.OK) Then
                dgWorkList.DataSource = Nothing
                ofdWorklist.Filter = "Xls files (*.xls)|*.XLS"
                ofdWorklist.CheckFileExists = True
                objWorkBook = objExcel.Workbooks.Open(ofdWorklist.FileName)
            Else
                'If the user clicks on Cancel in the OpenFileDialog, the dialog is closed
                ofdWorklist.Dispose()
            End If
            ' The contents of Worksheet(1) is being assigned to a worksheet
            objWorksheet = objWorkBook.Sheets(1)
         

            Try
           

            'dtExcelTable is table created to store the  lines from the excel.
            Dim objArray(objWorksheet.UsedRange.Columns.Count - 1) As String
            For excelColumns = 1 To objWorksheet.UsedRange.Columns.Count
                dtExcelTable.Columns.Add(objWorksheet.Cells(1, excelColumns).Text)
            Next
            For excelRows = 1 To objWorksheet.UsedRange.Rows.Count + 1
                For excelColumns = 1 To objWorksheet.UsedRange.Columns.Count
                    objArray(excelColumns - 1) = _
                    objWorksheet.Cells(excelRows, excelColumns).Text
                Next

                Dim columnCount As Int32
                columnCount = dtExcelTable.Columns.Count()
                dtExcelTable.Rows.Add(objArray)
                Array.Clear(objArray, 0, objArray.Length)
            Next
           
            'To assign the dtExcelTable to dtWorklistTable, which is the datatable bound
            'to the datagrid.
            dtWorklistTable = dsUpload.Tables("UploadTable")
           
            dsUpload.Tables(0).Dispose()
            'To kill the excel object created.
            objWorksheet = Nothing
            objWorkBook = Nothing
            objExcel.Quit()
            objExcel = Nothing
            GC.Collect()
        Catch
            If ofdWorklist.ShowDialog.Cancel Then
                Exit Sub
            End If
            MessageBox.Show("Please select an excel file to upload to the grid.", _
            Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End Try
    End Sub

From the dtWorklistTable  table u can insert values to the access database.
0
 
LVL 10

Accepted Solution

by:
123654789987 earned 500 total points
ID: 12234706
Insert to access database

Dim myConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\folder\FinanceDatabase.mdb"
'temporary hardcode the value to make sure it just can insert the record
myCommand.Connection.Open()
for i = 0 to dtWorkList.Rows.Count - 1
Dim myInsertCommand As String = "INSERT INTO Table (Column1, Column2, Column3) VALUES (dtWorkList.Rows(i)(0),
dtWorkList.Rows(i)(1),dtWorkList.Rows(i)(2))"


Dim myConnection As New OleDbConnection(myConnectionString)
Dim myCommand As New OleDbCommand(myInsertCommand, myConnection)


Try
intRowInsert = myCommand.ExecuteNonQuery() Catch ex As OleDbException
End Try
Next i
0
 

Author Comment

by:komlaaa
ID: 12237371
Thanks,
I forgot to mention this: my excel sheet is actually a remote database that i can connect to only by using a user name and password. How this part can be done.

i haven't test your answer yet but eveything looks perfect to me.

Thanks,... u r saving someone for keeping his job.


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert string to date 3 55
Get list of word ducuments in a folder 10 35
Advice in Xamarin 21 47
Pagebreak issue while printing the aspx page 3 11
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now