Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

read a CSV file and update an Access DB using ASP

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
samic400
Asked:
samic400
  • 2
1 Solution
 
amit_gCommented:
<%
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
 
samic400Author Commented:
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
 
amit_gCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now