简体   繁体   English

如何在浏览器中从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浏览器。

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

This would be really bad to do because sharing your connection string opens up your website to so many vulnerabilities that you can't simply patch up, you have to use a different method if you want it to be secure. 这样做真的很糟糕,因为共享你的连接字符串会打开你的网站到很多你不能简单修补的漏洞,如果你想要它是安全的,你必须使用不同的方法。 Otherwise you are opening up to a huge audience to take advantage of your site. 否则,您将向大量受众开放,以利用您的网站。

A perfect working code.. 完美的工作代码..

    <script>
    var objConnection = new ActiveXObject("adodb.connection");
    var strConn = "driver={sql server};server=QITBLRQIPL030;database=adventureworks;uid=sa;password=12345";
    objConnection.Open(strConn);
    var rs = new ActiveXObject("ADODB.Recordset");
    var strQuery = "SELECT * FROM  Person.Address";
    rs.Open(strQuery, objConnection);
    rs.MoveFirst();
    while (!rs.EOF) {
        document.write(rs.fields(0) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        document.write(rs.fields(1) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        document.write(rs.fields(2) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    ");
        document.write(rs.fields(3) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    ");
        document.write(rs.fields(4) + "<br/>");
        rs.movenext();
    }
</script>

Web services 网页服务

SQL 2005+ supports native WebServices that you could almost use although I wouldn't suggest it, because of security risks you may face. SQL 2005+支持您几乎可以使用的本机Web服务,尽管我不建议使用它,因为您可能面临安全风险。 Why did I say almost . 为什么我差点说。 Well Javascript is not SOAP native, so it would be a bit more complicated to actually make it. 好吧Javascript不是SOAP原生的,所以实际制作它会有点复杂。 You'd have to send and receive SOAP via XmlHttpRequest . 您必须通过XmlHttpRequest发送和接收SOAP。 Check google for Javascript SOAP clients. 检查谷歌的Javascript SOAP客户端。

Playing with JavaScript in an HTA I had no luck with a driver={SQL Server};... connection string, but a named DSN was OK : 在HTA中使用JavaScript我没有运气driver={SQL Server};...连接字符串,但命名的DSN是正常的:
I set up TestDSN and it tested OK, and then var strConn= "DSN=TestDSN"; 我设置了TestDSN并测试好了,然后var strConn= "DSN=TestDSN"; worked, so I carried on experimenting for my in-house testing and learning purposes. 工作,所以我继续进行实验,以进行内部测试和学习。

Our server has several instances running, eg server1\\dev and server1\\Test which made things slightly more tricky as I managed to waste some time forgetting to escape the \\ as \\\\ :) 我们的服务器有几个运行的实例,例如server1 \\ devserver1 \\ Test ,这使得事情稍微有点棘手,因为我设法浪费一些时间忘记逃避\\作为\\\\ :)
After some dead-ends with server=server1;instanceName=dev in the connection strings, I eventually got this one to work : 在连接字符串中使用server=server1;instanceName=dev一些死胡同之后,我最终得到了这个:
var strConn= "Provider=SQLOLEDB;Data Source=server1\\\\dev;Trusted_Connection=Yes;Initial Catalog=MyDatabase;"

Using Windows credentials rather than supplying a user/pwd, I found an interesting diversion was discovering the subtleties of Integrated Security = true v Integrated Security = SSPI v Trusted_Connection=Yes - see Difference between Integrated Security = True and Integrated Security = SSPI 使用Windows凭证而不是提供用户/密码,我发现一个有趣的转移是发现Integrated Security = true的微妙之处Integrated Security = true v Integrated Security = SSPI v Trusted_Connection=Yes - 请参阅集成安全性=真实和集成安全性之间的区别= SSPI

Beware that RecordCount will come back as -1 if using the default adOpenForwardOnly type. 请注意,如果使用默认的adOpenForwardOnly类型,RecordCount将返回-1 If you're working with small result sets and/or don't mind the whole lot in memory at once, use rs.Open(strQuery, objConnection, 3); 如果你正在处理小的结果集和/或不介意一次在内存中的全部,请使用rs.Open(strQuery, objConnection, 3); (3=adOpenStatic) and this gives a valid rs.RecordCount (3 = adOpenStatic) ,这给出了一个有效的rs.RecordCount

As stated before it shouldn't be done using client side Javascript but there's a framework for implementing what you want more securely. 如前所述,它不应​​该使用客户端Javascript,但有一个框架,可以更安全地实现您想要的。

Nodejs is a framework that allows you to code server connections in javascript so have a look into Nodejs and you'll probably learn a bit more about communicating with databases and grabbing data you need. Nodejs是一个允许您在javascript中编写服务器连接的框架,因此请查看Nodejs,您可能会学到更多关于与数据库通信和获取所需数据的知识。

(sorry, this was a more generic answer about SQL backends--I hadn't read the answer about SQL Server 2005's WebServices feature. Although, this feature is still run over HTTP rather than more directly via sockets, so essentially they've built a mini web server into the database server, so this answer is still another route you could take.) (对不起,这是一个关于SQL后端的更通用的答案 - 我没有读过有关SQL Server 2005的WebServices功能的答案。虽然,这个功能仍然通过HTTP运行而不是通过套接字直接运行,所以基本上他们已经构建了一个迷你Web服务器进入数据库服务器,所以这个答案仍然是你可以采取的另一条路线。)

You can also connect directly using sockets (google "javascript sockets") and by directly at this point I mean using a Flash file for this purpose, although HTML5 has Web Sockets as part of the spec which I believe let you do the same thing. 您也可以直接使用套接字(谷歌“javascript套接字”)直接连接,直接在这一点上我的意思是使用Flash文件为此目的,虽然HTML5有Web套接字作为规范的一部分,我相信让你做同样的事情。

Some people cite security issues, but if you designed your database permissions correctly you should theoretically be able to access the database from any front end, including OSQL, and not have a security breach. 有些人引用安全问题,但如果您正确设计了数据库权限,理论上您应该能够从任何前端访问数据库,包括OSQL,而不会出现安全漏洞。 The security issue, then, would be if you weren't connecting via SSL. 那么,如果您没有通过SSL连接,则会出现安全问题。

Finally, though, I'm pretty sure this is all theoretical because I don't believe any JavaScript libraries exist for handling the communications protocols for SSL or SQL Server, so unless you're willing to figure these things out yourself it'd be better to go the route of having a web server and server-side scripting language in between the browser and the database. 最后,我很确定这一切都是理论上的,因为我不相信任何用于处理SSL或SQL Server通信协议的JavaScript库,所以除非你愿意自己解决这些问题,否则它会是最好在浏览器和数据库之间使用Web服务器和服务器端脚本语言。

I dont think you can connect to SQL server from client side javascripts. 我不认为你可以从客户端javascripts连接到SQL服务器。 You need to pick up some server side language to build web applications which can interact with your database and use javascript only to make your user interface better to interact with. 您需要选择一些服务器端语言来构建可以与您的数据库交互的Web应用程序,并且仅使用javascript来使您的用户界面更好地与之交互。

you can pick up any server side scripting language based on your language preference : 您可以根据您的语言偏好选择任何服务器端脚本语言:

  • PHP PHP
  • ASP.Net ASP.Net
  • Ruby On Rails Ruby On Rails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在浏览器中使用来自 JavaScript 的 SOCKS5 协议连接到服务器? - How to connect to server using SOCKS5 protocol from JavaScript in browser? 如何通过Internet Explorer中运行的JavaScript连接到SQL CE数据库? - How to connect to a SQL CE database from JavaScript running in Internet Explorer? 如何使用 JavaScript 从 Azure Functions 连接到 Microsoft SQL Server? - How to connect to Microsoft SQL Server from Azure Functions using JavaScript? 如何使用Javascript和PHP从浏览器连接到WIFI? - How to connect to WIFI from a browser with Javascript and PHP? 如何将JavaScript连接到SQL Server? - How can i connect JavaScript to a SQL server? javascript-如何从SQL Server数据库创建日期和时间倒计时? - javascript - how to create countdown to date and time from sql server database? 我可以从浏览器中运行的JavaScript直接连接到Redis服务器吗? - Can I connect directly to a Redis server from JavaScript running in a browser? 如何从 javascript 连接到 pointbase 数据库? - How to connect to pointbase database from javascript? Javascript可以通过浏览器与远程服务器上的SQLite数据库进行交易吗? - Can Javascript transact with a SQLite database on a remote server from a browser? 如何从Javascript连接到XMPP服务器 - How to connect to XMPP server from Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM