Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i tried to get data from sql server database using javascript in html


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Database Connect</title>
    <script type="text/javascript">

        var connection = new ActiveXObject("ADODB.Connection");

        var connectionstring = "Data Source=GD-04\SQLEXPRESS;Initial Catalog=Employee;User ID=sa;Password=grassdew;Provider=SQLOLEDB";

        connection.open(connectionstring);
        var rs = new ActiveXObject("ADODB.Recordset");
        var strQuery = "SELECT * FROM Customer_Orders";
        rs.Open(strQuery, connection);
        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) + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
            document.write(rs.fields(5) + "<br/>");
            rs.movenext();
        }

        rs.close();
        connection.close();


    </script>
</head>
<body>
    <div id="main"></div>
</body>
</html>
Posted
Updated 25-Sep-16 0:30am
v3
Comments
F-ES Sitecore 21-Sep-16 5:06am    
You won't be able to do that in any modern browser, and even with older browsers you'll need to disable the security of the browser. Javascript is not suitable for accessing databases.
Suvendu Shekhar Giri 21-Sep-16 5:07am    
What is the issue here?
It's not recommended to use plain javascript to access databases considering security issues.
Still if you have reason to do so, please share the issue you are facing so that we'll try to guide you further.

1) ActiveXObject("ADODB.Recordset") is generally unavailable (certainly on all of my systems) - and 'spotty' availability makes it useless.

2) This will teach you how: PHP 5 Tutorial[^] PHP is a very widely available server-side language.

inside the tutorial they talk about accessing MySQL - there are plug-in drivers available from Microsoft for giving php access to SQL. Download Microsoft Drivers for PHP for SQL Server from Official Microsoft Download Center[^]

You'll notice that php isn't a whole lot different than javascript - at least in that it's follows C syntax, and if you've ever learned 'C' then you know a lot of it's library, already.


 
Share this answer
 
It is great memthod to get data, thank you a lot.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900