How to connect to SQL Server database from JavaScript in the browser?

前端 未结 8 2069
萌比男神i
萌比男神i 2020-11-22 00:56

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.

<
相关标签:
8条回答
  • 2020-11-22 01:35

    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. Why did I say almost. Well Javascript is not SOAP native, so it would be a bit more complicated to actually make it. You'd have to send and receive SOAP via XmlHttpRequest. Check google for Javascript SOAP clients.

    • http://msdn.microsoft.com/en-us/library/ms345123.aspx - SQL native WebServices
    • http://www.google.com/search?q=javascript+soap - Google results for Javascript SOAP clients
    0 讨论(0)
  • 2020-11-22 01:39

    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>
    
    0 讨论(0)
提交回复
热议问题