如何在浏览器中从 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 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)