ASP.NETではなく、レガシーASPの話...
「Microsoft Text Driver」というドライバを使うと、CSVファイルをDBテーブルのようにSQL(SELECT文)でアクセスできる。
この例ではASPファイルと同じ階層にあるdataフォルダをCSV格納フォルダとしている。
CSVファイルに直接アクセスされたくないケースが多いと思うが、その場合はIISの設定パネルでdataフォルダの読み取り権限をはずしておけば良い。
==================================================
<%@ Language=VBScript %>
<%
Option Explicit
Dim rs, strCon, strSql, fld, fldVal
Response.CharSet = "Shift_JIS"
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Cache-Control", "no-store"
Response.CacheControl = "Private"
Set rs = Server.CreateObject("ADODB.Recordset")
strCon = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & Server.MapPath(".") & "\data;"
strSql = "SELECT * FROM bank_account.csv WHERE [企業コード] <> 'A000000003' OR ([事業所コード] = '002') ORDER BY [企業コード] DESC"
rs.Open strSql, strCon, 0
%>
<html>
<head>
<title>CsvDb</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<style type="text/css"><!--
table.db {
font-size: 8pt;
}
--></style>
</head>
<body>
<div>
CsvDb<br />
<table class="db" border="1" cellspacing="1" cellpadding="2">
<%
Do While rs.EOF = False
Response.Write "<tr>" & VbCrLf
For Each fld In rs.Fields
fldVal = fld.Value
If IsNull(fldVal) Then
fldVal = " "
End If
Response.Write vbTab & "<td>" & fldVal & "</td>" & VbCrLf
Next
Response.Write "</tr>" & VbCrLf
rs.MoveNext
Loop
%>
</table>
</div>
</body>
</html>
<%
rs.Close
Set rs = Nothing
%>
==================================================
デフォルトではフィールドの型がCharのため、255文字以上の値は255文字で切れてしまう。
その場合は、下のようなschema.iniファイルをcsvファイルと同じフォルダに用意しておくと、読込むときの型を指定できる。
下はbank_account.csvというCSVファイルに対するスキーマ指定の例。
Col7のフィールドをLongChar型と指定しているので255文字以上を読み込める。
フィールドの型はJetSQLの解説を参照。
schema.iniの書き方はこちらが分かりやすい。
http://msdn.microsoft.com/ja-jp/library/ms404679.aspx
==================================================
[bank_account.csv]
ColNameHeader=True
Format=CSVDelimited
Col1=企業コード Char
Col2=事業所コード Char
Col3=全銀コード Char
Col4=全銀支店コード Char
Col5=口座区分 Char
Col6=口座番号 Char
Col7=備考 LongChar
Col8=有効フラグ Char
Col9=作成日 Char
Col10=作成者 Char
Col11=更新日 Char
Col12=更新者 Char
「Microsoft Text Driver」というドライバを使うと、CSVファイルをDBテーブルのようにSQL(SELECT文)でアクセスできる。
この例ではASPファイルと同じ階層にあるdataフォルダをCSV格納フォルダとしている。
CSVファイルに直接アクセスされたくないケースが多いと思うが、その場合はIISの設定パネルでdataフォルダの読み取り権限をはずしておけば良い。
==================================================
<%@ Language=VBScript %>
<%
Option Explicit
Dim rs, strCon, strSql, fld, fldVal
Response.CharSet = "Shift_JIS"
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "Cache-Control", "no-store"
Response.CacheControl = "Private"
Set rs = Server.CreateObject("ADODB.Recordset")
strCon = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & Server.MapPath(".") & "\data;"
strSql = "SELECT * FROM bank_account.csv WHERE [企業コード] <> 'A000000003' OR ([事業所コード] = '002') ORDER BY [企業コード] DESC"
rs.Open strSql, strCon, 0
%>
<html>
<head>
<title>CsvDb</title>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<style type="text/css"><!--
table.db {
font-size: 8pt;
}
--></style>
</head>
<body>
<div>
CsvDb<br />
<table class="db" border="1" cellspacing="1" cellpadding="2">
<%
Do While rs.EOF = False
Response.Write "<tr>" & VbCrLf
For Each fld In rs.Fields
fldVal = fld.Value
If IsNull(fldVal) Then
fldVal = " "
End If
Response.Write vbTab & "<td>" & fldVal & "</td>" & VbCrLf
Next
Response.Write "</tr>" & VbCrLf
rs.MoveNext
Loop
%>
</table>
</div>
</body>
</html>
<%
rs.Close
Set rs = Nothing
%>
==================================================
デフォルトではフィールドの型がCharのため、255文字以上の値は255文字で切れてしまう。
その場合は、下のようなschema.iniファイルをcsvファイルと同じフォルダに用意しておくと、読込むときの型を指定できる。
下はbank_account.csvというCSVファイルに対するスキーマ指定の例。
Col7のフィールドをLongChar型と指定しているので255文字以上を読み込める。
フィールドの型はJetSQLの解説を参照。
schema.iniの書き方はこちらが分かりやすい。
http://msdn.microsoft.com/ja-jp/library/ms404679.aspx
==================================================
[bank_account.csv]
ColNameHeader=True
Format=CSVDelimited
Col1=企業コード Char
Col2=事業所コード Char
Col3=全銀コード Char
Col4=全銀支店コード Char
Col5=口座区分 Char
Col6=口座番号 Char
Col7=備考 LongChar
Col8=有効フラグ Char
Col9=作成日 Char
Col10=作成者 Char
Col11=更新日 Char
Col12=更新者 Char