Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

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

Can anybody give me some sample source code showing how to connect to a SQL Server 2005 database from JavaScript locally?

(任何人都可以给我一些示例源代码,展示如何从本地JavaScript连接到SQL Server 2005数据库?)

I am learning web programming on my desktop.

(我在桌面上学习网络编程。)

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.

(如果你有它们,建议一些替代方案,但我现在尝试使用JavaScript。)

My SQL Server is locally installed on my desktop — SQL Server Management Studio 2005 and IE7 browser.

(我的SQL Server本地安装在我的桌面上 - SQL Server Management Studio 2005和IE7浏览器。)

  ask by Enjoy coding translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

(由于多种原因(不良做法,安全问题等),您不应该使用客户端javascript来访问数据库,但如果您真的想这样做,请举例说明:)

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; 

A better way to connect to a sql server would be to use some server side language like PHP, Java, .NET, among others.

(连接到SQL服务器的更好方法是使用一些服务器端语言,如PHP,Java,.NET等。)

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存在的古老传说,但这是另一个故事。)

;)

(;))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...