更新时间:2023-02-22 13:44:35
出于多种原因(不良做法、安全问题等),您不应该使用客户端 javascript 访问数据库,但如果您真的想这样做,这里是一个例子:
You shouldn´t use client javascript to access databases for several reasons (bad practice, security issues, etc) but if you really want to do this, here is an example:
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
document.write(rs.fields(1));
rs.movenext;
}
rs.close;
connection.close;
连接到 sql 服务器的更好方法是使用一些服务器端语言,如 PHP、Java、.NET 等.客户端 javascript 应仅用于接口.
A better way to connect to a sql server would be to use some server side language like PHP, Java, .NET, among others. Client javascript should be used only for the interfaces.
并且有关于服务器javascript存在的古老传说的传言,但这是另一回事.;)
And there are rumors of an ancient legend about the existence of server javascript, but this is another story. ;)