Reading of text databases from ASP

Text databases are the formatted text files with separators or as still it is necessary named files where each column of the data is divided{shared} with the help of the set separator (a point, a semicolon, etc.), and each line is divided{shared} by a symbol of a new line. This format is widely used for data exchange between applications, for example, you can save your table from Excel or Access as file CSV (comma separated file) and later to import the data from the received file to other application. Given clause{article} describes an example of connection with file CSV as with a database. The similar example can be useful by development online of service where the client can export the data from Excel to a database of a website by means of a CSV-file.


For the beginning we shall create file DSN the driver of text files (Microsoft Text Driver). It can be made with help ODBC32 in Control Panel or manually - create file CSV.DSN and place in him  the following text:

[ODBC]

DRIVER=Microsoft Text Driver (*.txt; *.csv)

UID=admin

UserCommitSync=Yes

Threads=3

SafeTransactions=0

PageTimeout=5

MaxScanRows=25

MaxBufferSize=512

ImplicitCommitSync=Yes

FIL=text

Extensions=txt, csv, tab, asc

DriverId=27



For job of an example we shall create a test file data.txt with the following data:

ID, Name, Price

1, "Chairs", $40.00

2, "Table", $75.00

3, "Fork", $1.50

4, "Lamp", $15.00

5, "Rug", $35.00



Now the following code we shall save as a ASP-script:

<html>

<body>


<%

Dim sDSNFile

sDSNFile = "CSV.dsn"


Dim sScriptDir

sScriptDir = Request. ServerVariables ("SCRIPT_NAME")

sScriptDir = StrReverse (sScriptDir)

sScriptDir = Mid (sScriptDir, InStr (1, sScriptDir, "/"))

sScriptDir = StrReverse (sScriptDir)


' We shall create DSN

Dim sPath, sDSN

sPath = Server. MapPath (sScriptDir) and " "

sDSN = « FileDSN = » both sPath and sDSNFile and _

«; DefaultDir = » and sPath and _

«; DBQ = » and sPath and ";"


' For example sDSN can it will turn out such:

' FileDSN=C:InetpubwwwrootCSV.DSN; DefaultDir =

' C:Inetpubwwwroot; DBQ=C:Inetpubwwwroot;


' We are connected to a DB

Dim Conn, rs

Set Conn = Server. CreateObject ("ADODB.Connection")

Conn. Open sDSN


' We shall receive only Name and Price

Dim sql

sql = « SELECT Name, Price FROM data.txt »


' We shall create object recordset

set rs = conn.execute (sql)


Do WHile Not rs. EOF

Response. Write " Name: " and rs ("Name")

Response. Write " <BR> Price: " and rs ("Price")

Response. Write "<HR>"

rs. MoveNext

Loop


' The termination{ending} of job

rs.close

set rs = nothing

conn.close

set conn = nothing

%>


<body>

</html>



All this. It is necessary to note, that for check of job of an example you should save all three files in one directory of your web - server.