如何从浏览器中的JavaScript连接到SQL

如何从浏览器中的JavaScript连接到SQL

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

问题描述

任何人都可以给我一些示例源代码,显示如何从本地连接到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数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 17:02