使用Microsoft文本驱动程序进行ODBC查询执行失败

时间:2014-04-24 19:43:16

标签: sql batch-file vbscript odbc jscript

这里我有一个创建odbc dns(Microsoft文本驱动程序(* .txt; * .csv))的批处理/ jscript文件,连接到它但是当我尝试执行查询时出现错误。这是可以直接运行和测试的.bat文件:

@if (@X)==(@Y) @end /* JScript comment
@echo off

rd /s /q C:\dbtest
md C:\dbtest
rem create a new "database"
(
echo "col1","col2"
echo "1","2"
) >>C:\dbtest\test.txt
odbcconf configdsn "Microsoft Text Driver (*.txt; *.csv)" "DSN=testdns1|description=test_dns_1|defaultdir=C:\dbtest"
 cscript //E:JScript //nologo "%~f0"

exit /b 0

*/
var rs = new ActiveXObject("ADODB.Recordset");
var comm=new ActiveXObject("ADODB.Command");
var conn = new ActiveXObject("ADODB.Connection");
conn.ConnectionString="testdns1";
// read write
conn.Mode=3

conn.Open;
conn.Execute("Select  * FROM test");
conn.Close();

对于64位机器:

@if (@X)==(@Y) @end /* JScript comment
@echo off
setlocal
path %windir%\sysWOW64\
rd /s /q C:\dbtest
md C:\dbtest
rem create a new "database"
(
echo "col1","col2"
echo "1","2"
) >>C:\dbtest\test.txt
odbcconf configdsn "Microsoft Text Driver (*.txt; *.csv)" "DSN=testdns1|description=test_dns_1|defaultdir=C:\dbtest"
 cscript //E:JScript //nologo "%~f0"
endlocal
exit /b 0

*/


var rs = new ActiveXObject("ADODB.Recordset");
var comm=new ActiveXObject("ADODB.Command");
var conn = new ActiveXObject("ADODB.Connection");
conn.ConnectionString="testdns1";
// read write
conn.Mode=3

conn.Open;
conn.Execute("Select  * FROM test");
conn.Close();

但在conn.Execute("Select * FROM test");我收到以下错误:

 Microsoft OLE
DB Provider for ODBC Drivers: [Microsoft][ODBC Text Driver] The Microsoft Jet da
tabase engine cannot open the file '(unknown)'.  It is already opened exclusivel
y by another user, or you need permission to view its data.

activex对象是使用jscript创建的,但与vbscript相同(我甚至使用vbscript示例作为参考)。

知道什么可能导致此错误吗?因为文件只是从同一个用户创建而没有人同时使用它

1 个答案:

答案 0 :(得分:1)

您的表名是test.txt。所以改变

conn.Execute("Select  * FROM test");

conn.Execute("Select  * FROM [test.txt]");