views:

44

answers:

1

Hi,

I have been asked to build a web application to report on information stored in another system. The other system is locked down but will allow me to export data as a csv file.

I'd like to use an html form on my application so that people (the night shift!) can import data from the other system to my web application.

To allow other people to understand my code I've been using vbscript and trying to use the following pattern in all database operations:

Open Connection Build Query Execute Query Do something with results (if appropriate) Close Connection

Is it possible to use this pattern to import a txt file? ie. purely sql based without using store procedures?

Does anyone have a code example?

Hope that makes sense.

Thanks.

Derek

+2  A: 

Some notes.

Set cn = CreateObject("ADODB.Connection")
''SQL Server Express and ODBC, more connection strings: 
''http://www.connectionstrings.com/sql-server-2008
''
strcon = "ODBC;Description=Test;DRIVER=SQL Server;SERVER=Server\SQLEXPRESS;"
strcon = strcon & "Trusted_Connection=Yes;DATABASE=Test"

cn.Open strcon
strSQL = "SELECT * INTO NewCSV "
strSQL = strSQL & "FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',"
strSQL = strSQL & "'Text;HDR=Yes;FMT=Delimited;DATABASE=c:\docs\', "
strSQL = strSQL & "'SELECT * FROM [Test.csv]');"

cn.Execute strSQL, RecordsAffected
MsgBox RecordsAffected

You may have to enable ad hoc queries: http://technet.microsoft.com/en-us/library/ms187569.aspx It is also possible to use the ACE provider for text, but it may get complicated: http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/ace

Mixed data in columns can be a problem with CSV. IMEX can help, but only if the range checked, which is set in the registry, is suitable.

EDIT re comments

Some notes on viewing CSV data:

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Docs\;"
strcon = strcon & "Extended Properties=""Text;FMT=Delimited;HDR=Yes;IMEX=1"";"

cn.Open strcon

strSQL = "Select * From [Test.csv]"

rs.Open strSQL, cn

MsgBox rs.GetString
Remou
Thanks for the reply Remou. Have tried this and get the error 'Ad hoc access to OLEDB provider Microsoft.Jet.OleDB.4.0 has been denied. You must access this provider through a linked server'. I suspect because the only server I have access to is a corporate one and they've locked everything down? I've tried BULK INSERT too and got a similar response. I read somewhere that parsing the file myself is too much of a headache but feel it might be the only way?!
Derek
I am confused. You mean you cannot enable ad hoc queries on the server into which you are importing the CSV file, that is, your web application is not yours ? Are the two things the same, the server from which you are deriving the CSV file and into which you are importing the file?
Remou
Indeed. It's on the corporate network and I can ftp to the server and either query the database using ms access or asp pages on the server.
Derek
Sorry pressed return before I'd finished. The csv is derived from another remote server but the csv can be saved on a local machine so the idea was to use the local machine as a bridge between two remote and fairly tightly controlled external servers. I'll try asking the server people nicely to enable ad hoc queries but I'm not optimistic.
Derek
What can you use to update the server? MS Access?
Remou
I can edit data with access and add tables with the 'upsizing wizard' but other than that not much. I hoped there might be an easy way to handle csv's similar to php's fgetcsv.
Derek
If you just want to open the csv as a table in VBScript, you can simply use a Jet connection and a recordset, however, if you want to update or add an SQL Server table, it is easiest if you enable Ad Hoc queries.
Remou
Remou you are an absolute legend. Have the csv in a nice manageable recordset now and can play arouind with it to my heart's content. Thanks a million.
Derek