Solved

Inserting into one-to-many tables

Posted on 2004-10-05
3
205 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

10 Experts available now in Live!

Get 1:1 Help Now