0

I've been perusing some hep forums and some help books but cant seem to get my head wrapped around this. My task is to read data from two text files and then load that data into an existing MS Access 2007 database. So here is what i'm trying to do:

  1. Read data from first text file and for every line of data add data to a DataTable using CarID as my unique field.
  2. Read data from second text file and look for existing CarID in DataTable if exists update that row. If it doesnt exist add a new row.
  3. once im done push the contents of the DataTable to the database.

What i have so far:

    Dim sSQL As String = "SELECT * FROM tblCars"
    Dim da As New OleDb.OleDbDataAdapter(sSQL, conn)
    Dim ds As New DataSet
    da.Fill(ds, "CarData")
    Dim cb As New OleDb.OleDbCommandBuilder(da)

    'loop read a line of text and parse it out. gets dd, dc, and carId

    'create a new empty row
    Dim dsNewRow As DataRow = ds.Tables("CarData").NewRow()

    'update the new row with fresh data
    dsNewRow.Item("DriveDate") = dd
    dsNewRow.Item("DCode") = dc
    dsNewRow.Item("CarNum") = carID
    'about 15 more fields

    'add the filled row to the DataSet table
    ds.Tables("CarData").Rows.Add(dsNewRow)

    'end loop

    'update the database with the new rows
    da.Update(ds, "CarData")

Questions:

In constructing my table i use "SELECT * FROM tblCars" but what if that table has millions of records already. Is that not a waste of resources? Should i be trying something different if i want to update with new records?

Once Im done with the first text file i then go to my next text file. Whats the best approach here: To First look for an existing record based on CarNum or to create a second table and then merge the two at the end?

Finally when the DataTable is done being populated and im pushing it to the database i want to make sure that if records already exist with three primary fields (DriveDate, DCode, and CarNum) that they get updated with new fields and if it doesn't exist then those records get appended. Is that possible with my process?

tia AGP

1
  • Unless I am missing something, I would not use an intermediary DataTable, but insert or update the MS-Access database directly from the text file data. First test to see if the key exists and insert or update accordingly. Commented Jul 10, 2013 at 23:31

1 Answer 1

1

Loading every text file into memory is the better performing option, and easier to code. This is of course fully dependent on how much memory you have available, and how big your text files are.

My approach would be to first load all of the data from the files into a DataTable. Then convert this table into XML. You can then pass the XML into a Stored Procedure. This stored procedure will convert the XML into either a Table Variable or Temporary table that you can run SQL queries off of.

From here it’s a simple case of doing a “Not Exists” query in your SP on tblCars with the data you’ve passed in, and inserting were applicable.

In my mind this is by far the best performing option, there’s no need for your application to pull any data out of SQL. SQL is optiomized for these kinds of things and will out perform a application hugely.

If you wanted to get really clever about it you could read each text file using a worker thread, and load them into SQL as soon as they’ve been read. It would save on memory usage and be very fast.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip. The reading of the text files is very minor as its only a 100 lines of text I'm reading in. The main part Im doing right now is updating the database with new records. For that it seems to me the DataAdapter/DataTable is the easiest way to go but am just in the dark as to why i have to pull all the data first. btw this is for an MS Access database.
What does "load into SQL" mean? SQL Server is not involved here so far as I know, so I'm having trouble parsing the intended meaning.
I think someone else said that. My original question was loading text data into MS Access using SQL commands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.