Solved

read a CSV file and update an Access DB using ASP

Posted on 2006-10-26
3
214 Views
Last Modified: 2012-05-05
I have done a ton of database programming using SQL Server, Access and ASP.

I have a new challenge now.

I need to build an ASP script where I read a CSV file, break out the fields and then update my DB using ASP. Has anyone done anything like this before? I'm thinking it is fairly easy maybe using something like the "split" function?

Looking for ideas on how to accomplish this and a small snippet of code if possible.'

Thanks!
0
Comment
Question by:samic400
  • 2
3 Comments
 
LVL 58

Expert Comment

by:amit_g
ID: 17815884
<%
dim oConn, sConnectionString, sSQL
set oConn = Server.CreateObject("ADODB.Connection")
sConnectionString = "Provider=SQLOLEDB.1;Initial Catalog=YourDBName;Data Source=ServerName;User Id=Username;PASSWORD=Password;"
Call oConn.Open(sConnectionString)

sSQL = "bulk insert TheDestinationTable from '\\WhateverPlace\WhateverDir\TheFileName' WITH (DATAFILETYPE = 'char', FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')"

Call oConn.Execute(sSQL)

Call oConn.Close()
Set oConn = Nothing
%>

Just make sure that

\\WhateverPlace\WhateverDir\TheFileName

is either a UNC path accessible to SQL server or a path accessible from SQL server.
0
 
LVL 13

Author Comment

by:samic400
ID: 17815910
Would it be the same for an access DB? that is what I will be updating in this case.

I have to actually read record by record because if a certain record already exists in the DB, then I don't want to write to the table.

I'm guessing that I would need to read the text file, row by row, strip out the commas, if my key field doesn't exist, then write the record.

Do the same until i've read through the entire file.

0
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 17816618
No this would not work for MS Access. But if you have a SQL server available, do the above and load the data in SQL server. Then do regular query against SQL server and load the data in MS Aceess. That would be much easier. But if you do want to do it directly agains the text file and MS Access, use this connection string to open an ADO connection to text file and then you could use it as if it were a daatabase table.

sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TheFileFolder\;Extended Properties=""text;HDR=Yes;FMT=Delimited"""

Call oConn.Open(sConnectionString)

sSQL = "select * from TheFileName"

Set oRs = oConn.Execute(sSQL)

While Not oRs.EOF
    sField1 = oRs(0)
    sField2 = oRs(2)
    ...
Wend
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Recordset containing single quotes 8 36
Select only the top record in a left join 13 37
Button to go back 3 25
html Uncheck Checkbox 2 13
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 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