Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading from a text file and then storing into the database

Reading from a text file and then storing into the database

Scheduled Pinned Locked Moved C#
questiondatabasesql-serversysadminhelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mhmo
    wrote on last edited by
    #1

    Hi all I want to write a program that will read a text file as an unput. This file will contain some info for students, one student in one line. It might contain 60,000 to 500,000 lines. Now the program shoul read each line and store the info. into the database. My question is: What is the fastest way for doing that? inserting the data into the database must be through the program(not importing the data through the SQL Server or MS Access). Would u help me please :)

    R U 2 Replies Last reply
    0
    • M mhmo

      Hi all I want to write a program that will read a text file as an unput. This file will contain some info for students, one student in one line. It might contain 60,000 to 500,000 lines. Now the program shoul read each line and store the info. into the database. My question is: What is the fastest way for doing that? inserting the data into the database must be through the program(not importing the data through the SQL Server or MS Access). Would u help me please :)

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      First of all you will need to open a connection to the database. Depending on which db you want to write to choose one of the providers coming with the framework (OleDb, ODBC...). If you need it fast you'll probably have to test them. If its possible I would define a stored procedure in the database. Otherwise you will have to make a parameterized query. Once this is done the reading can begin. You will now need a FileStream to open the file and read it line by line. Depending on the format you will have to split each line some way and fill in the parameters to your query. Execute it and go fopr the next line. Thats it. :)

      M 1 Reply Last reply
      0
      • M mhmo

        Hi all I want to write a program that will read a text file as an unput. This file will contain some info for students, one student in one line. It might contain 60,000 to 500,000 lines. Now the program shoul read each line and store the info. into the database. My question is: What is the fastest way for doing that? inserting the data into the database must be through the program(not importing the data through the SQL Server or MS Access). Would u help me please :)

        U Offline
        U Offline
        Uri Lavi
        wrote on last edited by
        #3

        If you are going to work with SQL Server The fastest way will be to use DTS. Uri

        M 1 Reply Last reply
        0
        • R Robert Rohde

          First of all you will need to open a connection to the database. Depending on which db you want to write to choose one of the providers coming with the framework (OleDb, ODBC...). If you need it fast you'll probably have to test them. If its possible I would define a stored procedure in the database. Otherwise you will have to make a parameterized query. Once this is done the reading can begin. You will now need a FileStream to open the file and read it line by line. Depending on the format you will have to split each line some way and fill in the parameters to your query. Execute it and go fopr the next line. Thats it. :)

          M Offline
          M Offline
          mhmo
          wrote on last edited by
          #4

          Thanks But, would u tell me more about parameterized query and give me some resources or examples. And another Q, do u prefer to insert the data line by line or first read as much as I can from the file and the insert them together. :)

          R 1 Reply Last reply
          0
          • M mhmo

            Thanks But, would u tell me more about parameterized query and give me some resources or examples. And another Q, do u prefer to insert the data line by line or first read as much as I can from the file and the insert them together. :)

            R Offline
            R Offline
            Robert Rohde
            wrote on last edited by
            #5

            1. Have a look here. Its in VB.Net and originally meant for ASP. But the same techniques apply for C# windows applications. The sample there could give you a good start. As you want to load data into the database and not read the only really difference is that you will have to use ExecuteQuery instead of ExecuteReader. 2. When using parameterized queries you will have to insert the data line by line. Thus there is no real reason to load more than line at a time into memory. In order to increase you could read your data in one thread (storing many lines in memory) and writing them to the database in another thread. This could improve performance in certain situations, but is also a bit advanced, so you probably should stick to the line by line handling.

            1 Reply Last reply
            0
            • U Uri Lavi

              If you are going to work with SQL Server The fastest way will be to use DTS. Uri

              M Offline
              M Offline
              mhmo
              wrote on last edited by
              #6

              Thanks, But what is DTS?

              U 1 Reply Last reply
              0
              • M mhmo

                Thanks, But what is DTS?

                U Offline
                U Offline
                Uri Lavi
                wrote on last edited by
                #7

                DTS is a part of the SQL Server and it states for: Data Transformation Services. It is particularly handy when you want to perform a task of transferring a data from text files, excels or other databases. DTS will be significantly faster than other program under assumption that there is no special business logic for the data to be transferred (Conversion of data isn’t considered to be a special business logic and can be easily done by the DTS). Hope it helps,

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups