VBScript + ADO : 汎用CSV出力

  ODBC の動的接続





Adocsv

IE8 では、TYPE=FILE で入力不可になっていたので、テキストフィールドを重ねて使っています。データの転送は、onchange イベントで転送している為、同じファイルを選択すると、転送されないので注意して下さい。

ADO を使って、select 文より自動的に CSV を出力するのはそんなに難しくありませんが、アプリケーションとして成り立たせる為に、いちいち DSN を作成せずに、動的に構成する接続文字列を作成する必要があります。

その場合、ODBC ドライバ文字列が必要になるので、レジストリから列挙して取得します。その際 WMI でしか無理ですが、コードは ODBC ドライバの列挙 を参照してください

Adocsv2

接続文字列作成の為のコードは以下のようになります

idx = document.getElementById("odbclist").selectedIndex

strDriver = "{" & document.getElementById("odbclist").options(idx).text & "}"
strServer = document.getElementById("p03").value
strUser = document.getElementById("p01").value
strPass =  document.getElementById("p02").value
strTable =  document.getElementById("p04").value
strDb =  document.getElementById("db_name").value
strFile =  document.getElementById("db_file").value

ConnectionString = _
	"Provider=MSDASQL" & _
	";Driver=" & strDriver

if strFile <> "" then
	ConnectionString = ConnectionString & ";Dbq=" & strFile
end if
if strServer <> "" then
	ConnectionString = ConnectionString & ";Server=" & strServer
end if
if strDb <> "" then
	ConnectionString = ConnectionString & ";Database=" & strDb
end if
if strUser <> "" then
	ConnectionString = ConnectionString & ";UID=" & strUser
end if
if strPass <> "" then
	ConnectionString = ConnectionString & ";PWD=" & strPass
end if

ConnectionString = ConnectionString & ";"

if document.getElementById("connectopt").value <> "" then
	ConnectionString = _
		ConnectionString & document.getElementById("connectopt").value & ";"
end if

MSDASQL は、ODBC 用のプロバイダですが、詳細は Microsoft OLE DB Provider for ODBC を参照してください。

接続文字列は、DB 固有のものがありますが、上記コード中の Dbq は、そのうちの一つで、Microsoft 系のファイルとして存在するデーターベース( MDB、Excel ) で使用します。これに関する参考ページはダウンロードの詳細 : 2007 Office system ドライバ: データ接続コンポーネント を参照して下さい。

MySQL では、ODBC 設定ダイアログから HELP を表示するか、MySQL 5.1 Connector/ODBC Connection Parameters を参照します












  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ