如何在浏览器中从 JavaScript 连接到 SQL Server 数据库?

How to connect to SQL Server database from JavaScript in the browser?(如何在浏览器中从 JavaScript 连接到 SQL Server 数据库?)

本文介绍了如何在浏览器中从 JavaScript 连接到 SQL Server 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一些示例源代码,展示如何从本地 JavaScript 连接到 SQL Server 2005 数据库?我正在台式机上学习网络编程.

Can anybody give me some sample source code showing how to connect to a SQL Server 2005 database from JavaScript locally? I am learning web programming on my desktop.

或者我需要使用任何其他脚本语言吗?如果你有的话,建议一些替代方案,但我现在正在尝试用 JavaScript 来做.我的 SQL Server 本地安装在我的桌面上——SQL Server Management Studio 2005 和 IE7 浏览器.

Or do I need to use any other scripting language? Suggest some alternatives if you have them, but I am now trying to do it with JavaScript. My SQL Server is locally installed on my desktop — SQL Server Management Studio 2005 and IE7 browser.

推荐答案

出于多种原因(不良做法、安全问题等),您不应该使用客户端 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. ;)

这篇关于如何在浏览器中从 JavaScript 连接到 SQL Server 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在浏览器中从 JavaScript 连接到 SQL Server 数据库?

基础教程推荐