Solved

Inserting into one-to-many tables

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Programmatically signing Word macros 4 72
vb.net 2 43
VB.NET 2008 (3.5 Framework) Remove all items from List 3 21
VB.NET - Error - Object not set in Module 4 33
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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