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

2023-07-17数据库问题
2

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

The End

相关推荐

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12

sql group by 与不同
sql group by versus distinct(sql group by 与不同)...
2024-04-16 数据库问题
37

如何在SQL中返回每个组的增量组号
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)...
2024-04-16 数据库问题
8

统计分组返回的记录数
Count number of records returned by group by(统计分组返回的记录数)...
2024-04-16 数据库问题
10

带聚合函数的 SQL GROUP BY CASE 语句
SQL GROUP BY CASE statement with aggregate function(带聚合函数的 SQL GROUP BY CASE 语句)...
2024-04-16 数据库问题
23