JSCRIPT连接ado

发布时间 2023-08-15 20:06:40作者: 小风风的博客

// JavaScript source code
// JScript source code

var console = {
log: function (txt)
{
WScript.Echo(txt);
}
}

function Recordset(h)
{
this.commandText = "";
this.activeConnection = null;
this.handle = h;
this.open = function ()
{
if (this.handle == null) {
this.handle = new ActiveXObject("adodb.Recordset");
}

return this.handle.Open(this.commandText, this.activeConnection, 3, 4,1);
}
this.get = function (id) { return this.handle(id); }
this.set = function (id, data){ return this.handle(id) = data; }
this.update = function () { this.handle.Update();}
this.close = function () { if (this.handle != null) { return this.handle.Close(); } }
this.recordCount = function ()
{
return this.handle == null ? -1 : this.handle.RecordCount;
}
this.eof = function () { return this.handle.EOF; }
this.moveNext = function () { return this.handle.MoveNext(); }
this.addNew = function () { if (this.handle !=null) { return this.handle.AddNew(); } }
}

function Connection()
{
this.commandText = "";
this.connectionString = "Provider=SQLOLEDB.1;Data Source=129.9.1.100,1433;Network Library=DBMSSOCN; Initial Catalog=test;User ID=sa;Password=00000;";
this.handle = new ActiveXObject("adodb.connection");
this.open = function () { return this.handle.Open(this.connectionString); }
this.close = function () { return this.handle.Close(); }
this.execute = function () { return this.handle.Execute(this.commandText); }
this.query = function () { var rs = this.handle.Execute(this.commandText); return new Recordset(rs); }
}

try
{
var db = new Connection();
db.open();
//db.commandText = "select * from sys_app";
//var rs = db.query();
//var n = rs.recordCount();

//while (!rs.eof())
//{
// console.log(rs.get("appname"));
// rs.moveNext();

//}

//rs.close();
//console.log("Rows:" + n.toString());

var r = new Recordset();
r.activeConnection = db.handle;
r.commandText = "select * from sys_app ";
r.open();
r.addNew();
r.set("appcode",'J001');
r.update();
//var r = new Recordset();
//r.activeConnection = db.handle;
//r.commandText = "select * from sys_app";
//r.open();
//while (!r.eof())
//{
// console.log(r.get("appname"));
// r.moveNext();

//}

//console.log("Rowcount:" + r.recordCount());
r.close();

db.close();
}catch(e)
{
console.log( e.description);
}