Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Import situation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Resolved [RESOLVED] [2005] Import situation

    Hi, i have an excel file, where there is a specific template which contains the data of products of a certain company.

    I am new and using visual basic 2005, as well as microsoft sql server 2000. What i wanted to do is i would like to import the excel items into the database through visual basic 2005. i would like to know the process and how to do the importing of excel items into the system and then to the database. Thank you.

    i would appreciate any help please.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Import situation

    First up, you should be aware that SQL Server will import data directly from Excel files.

    If you really do want to a VB app then it's simple ADO.NET. Follow the Data Access link in my signature for some code examples. In your case you'll have a DataTable for each worksheet in the Excel file. You'll use an OleDbDataAdapter to populate the DataTable. You'll then use a DataAdapter to save the contents of the DataTable to SQL Server. You might want to use a SqlDataAdapter or you could use the same OleDbDataAdapter you used to get the data. Either way, the SelectCommand of the first adapter and the InsertCommand of the second adapter will use different connections for the different databases.

    Another point to note is that the first adapter must have its AcceptChangesDuringFill property set to False, or else the data will not insert.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Import situation

    E.g.

    With one DataAdapter:
    vb.net Code:
    1. Dim excelConnection As New OleDbConnection("connection string here")
    2. Dim adapter As New OleDbDataAdapter("SELECT statement here", excelConnection)
    3.  
    4. adapter.AcceptChangesDuringFill = False
    5.  
    6. Dim table As New DataTable
    7.  
    8. 'Retrieve the data.
    9. adapter.Fill(table)
    10.  
    11. Dim sqlConnection As New OleDbConnection("connection string here")
    12. Dim insert As New OleDbCommand("INSERT statement here", sqlConnection)
    13.  
    14. 'Add parameters to insert here.
    15.  
    16. adapter.InsertCommand = insert
    17.  
    18. 'Save the data.
    19. adapter.Update(table)
    With two:
    vb.net Code:
    1. Dim excelConnection As New OleDbConnection("connection string here")
    2. Dim excelAdapter As New OleDbDataAdapter("SELECT statement here", excelConnection)
    3.  
    4. excelAdapter.AcceptChangesDuringFill = False
    5.  
    6. Dim table As New DataTable
    7.  
    8. 'Retrieve the data.
    9. excelAdapter.Fill(table)
    10.  
    11. Dim sqlConnection As New SqlConnection("connection string here")
    12. Dim sqlAdapter As New SqlDataAdapter
    13. Dim insert As New SqlCommand("INSERT statement here", sqlConnection)
    14.  
    15. 'Add parameters to insert here.
    16.  
    17. sqlAdapter.InsertCommand = insert
    18.  
    19. 'Save the data.
    20. sqlAdapter.Update(table)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Re: [2005] Import situation

    Thanks you for this great information. i will study what you've mentioned and work this out! Now i have a clear direction of what i will do! thank you!!

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Re: [2005] Import situation

    one more question, does the SELECT statement used in the Excel the same with the SELECT statement used in SQL? Thank you.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Import situation

    It IS SQL code, but it must be SQL syntax that is supported by the ADO.NET provider and data source that you're using. If you do the reading that has already been suggested then you'll have your answer. That Data Access Examples thread of mine provides a link to a site that provides the connection string format for various data sources, including Excel. That site also provides the precise SQL syntax for querying Excel.

    One point to note is that, if you don't have column headers in your Excel sheet then your column names default to F1, F2, F3, etc. I'm fairly sure that's right, although it might be C1, C2, C3, etc. A quick test will tell you, although, if you do have column headers, it doesn't matter anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2008
    Posts
    129

    Smile Re: [2005] Import situation

    Thank you for the info u gave, i was able to connect to an excel file, and able to view it in a datagridview! Now my next task is to import all the data from the excel file and append the table that will store it.

    The situation is like this; The excel file list the products of a company, that contains its item code, description, category, subcategory, price, etc. These new format of items are to be imported to an existing table in the SQL server 2000 database. Somebody told me to use BULK import of excel as well as VBNET. What do you think about it? Is there a better and more efficient way of approaching this kind of problem?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width