24-8 ???庫??

g WSHAڭ̤]iHƮwisWBקBRʧ@Aoǰʧ@]a SQL OӹFC|ҨӻAYn test.mdb iCAY ASPAiUCdҡG

Example]listdb01.asp^G

WzdҪlɦpUG

lɡ]listdb01.asp^G]ǦϰUYi^
<%@ language="jscript" %>
<% title="H JScript iƮwC" %>
<!--#include file="head.inc"-->
<hr>

<%
//====== Step 1Gإ߸ƮwsAM}ҸƮw
Conn = Server.CreateObject("ADODB.Connection");
Conn.ConnectionString = "DBQ=" + Server.MapPath("test.mdb") + ";Driver={Microsoft Access Driver (*.mdb)};Driverld=25;FIL=MS Access;";
Conn.Open();

//====== Step 2GSQLOAñNdߵGxs Recordset 
SQL = "Select * from testTable";	//qƪ testTable XҦ
RS = Conn.Execute(SQL);
%>

<table border=1 align=center>
<tr bgcolor="cyan">
<%
//====== Step 3GzL RecordSet Xo쪺e
//LXW
for (i=0; i<RS.Fields.Count; i++)
	Response.write("<th>"+RS(i).Name+"</th>\n");
%>
</tr>
<%
//LXC@
while (!RS.EOF) {
	Response.write("<tr>\n");
	for (i=0; i<RS.Fields.Count; i++)
		Response.write("<td>"+RS(i)+"&nbsp;</td>\n");
	RS.MoveNext();
}
%>
</table>

<%
//====== Step 4G RecordSet θƮws
RS.Close();
Conn.Close();
%>

<hr>
<!--#include file="foot.inc"-->

Y WSH ӹƮwCA{XܱApUG

lɡ]dbList01.js^G]ǦϰUYi^
// ϥ WSH CXƮwe

//====== Step 1Gإ߸ƮwsAM}ҸƮw
database="test.mdb";
conn = WScript.CreateObject("ADODB.Connection");
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database; 
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+database;	// 64-bit Office
conn.Open();

//====== Step 2GSQLOAñNdߵGxs recordset 
recordSet = WScript.CreateObject("ADODB.RecordSet"); 
sql = "SELECT * FROM testTable"; //qƪ test XҦ
recordSet.Open(sql, conn, 3, 3); 

//====== Step 3GzL recordSet Xo쪺e
//LXW
WScript.Echo("W١G");
for (i=0; i<recordSet.Fields.Count; i++)
	WScript.StdOut.Write(recordSet(i).Name+"\t");
WScript.Echo("");

//LXC@
i=1;
WScript.Echo("C@ơG");
while (!recordSet.EOF){
	for (j=0; j<recordSet.Fields.Count; j++)
		WScript.StdOut.Write(recordSet(j)+"\t");
	WScript.StdOut.Write("\n");
	i++;
	recordSet.MoveNext();
}

//====== Step 4G recordSet θƮws
recordSet.Close();
conn.Close();

ucscript dbList01.jsvAb DOS ROLXGpUG

W١G ssn account name team score C@ơG 14 abc μz 5 33.33 15 ABC LF 5 55.65 17 jtchen 3 48.76 18 aBc W 1 67.88 YnƮwisWAiUCdҡG

lɡ]dbInsert01.js^G]ǦϰUYi^
// ϥ WSH sWƮwe

//====== Step 1Gإ߸ƮwsAM}ҸƮw
database="test.mdb";
Conn = WScript.CreateObject("ADODB.Connection");
Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database; 
Conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+database;	// 64-bit Office
Conn.Open();

//====== Step 2Gإ SQL ROð椧
SQL="INSERT INTO testTable ([account], [name]) VALUES ('new1', 'new2')";
Conn.Execute(SQL);

//====== Step 3G RecordSet θƮws
Conn.Close();

pGAoɭԦA dbList01.jsAN|oıƤwghF@C

SOn`NOASQL O where 󦡬OjpgAҥHpGA󦡬O name='abc'AoɭԧXӪƥi]t 'abc'B'ABC'B'aBc' ơAYnѨMDAiHϥ strcomp ơAШUCdҡG

lɡ]dbList02.js^G]ǦϰUYi^
// jpgOƮwd
sql="select * from testTable where account='abc'";
WScript.Echo("jpgGsql = "+sql);
WScript.Echo("ﵲGG");
sql2screen("test.mdb", sql); 
sql="select * from testTable where strcomp(account, 'abc',0)=0";
WScript.Echo("jpgOGsql = "+sql);
WScript.Echo("ﵲGG");
sql2screen("test.mdb", sql); 

// ====== Function definitions
function sql2screen(database, sql){
	conn = WScript.CreateObject("ADODB.Connection"); 
//	conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database;
	conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+database;	// 64-bit office
	conn.Open();
	rs = WScript.CreateObject("ADODB.RecordSet"); 
	rs.Open(sql, conn, 3, 3);

	// LXW
	for (i=0; i<rs.Fields.Count; i++)
		WScript.StdOut.Write(rs(i).Name+"\t");
	WScript.StdOut.Write("\n");
	// LXC
	while (!rs.EOF){
		for (j=0; j<rs.Fields.Count; j++)
			WScript.StdOut.Write(rs(j)+"\t");
		WScript.StdOut.Write("\n");
		rs.MoveNext();
	}
	rs.Close();
	conn.Close();
}

LXGpUG

jpgGsql = select * from testTable where account='abc' ﵲGG ssn account name team score 14 abc μz 5 33.33 15 ABC LF 5 55.65 18 aBc W 1 67.88 jpgOGsql = select * from testTable where strcomp(account, 'abc',0)=0 ﵲGG ssn account name team score 14 abc μz 5 33.33 bWzlXAsql2screen() ƪ\ON SQL OGCLbùWCt@ӱ`ΪƬON SQL OGObɮפAbHUdҤAsql2file() ƪ\YOpG

lɡ]dbList03.js^G]ǦϰUYi^
// N testTable ƪexs output.txt
WScript.Echo("N testTable ƪexs output.txt ...");
sql2file("test.mdb", "select * from testTable", "output.txt");

// ====== Function definitions
function sql2file(database, sql, file){
	conn = WScript.CreateObject("ADODB.Connection"); 
//	conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+database;
	conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+database;	// 64-bit office
	conn.Open();
	rs = WScript.CreateObject("ADODB.RecordSet"); 
	rs.Open(sql, conn, 3, 3);

	fso = WScript.CreateObject("Scripting.FileSystemObject")
	fid = fso.CreateTextFile(file, true);

	// LXW
	for (i=0; i<rs.Fields.Count; i++)
		fid.Write(rs(i).Name+"\t");
	fid.Write("\r\n");
	// LXC
	while (!rs.EOF){
		for (j=0; j<rs.Fields.Count; j++)
			fid.Write(rs(j)+"\t");
		fid.Write("\r\n");
		rs.MoveNext();
	}
	fid.Close();
	rs.Close();
	conn.Close();
}

HW{Aɮ output.txt epUG

lɡ]output.txt^G]ǦϰUYi^
ssn	account	name	team	score	
14	abc	μz	5	33.33	
15	ABC	LF	5	55.65	
17	jtchen		3	48.76	
18	aBc	W	1	67.88	


JScript {]pPΡGΩ WSH