How to connect to SQL Server database from JavaScript in the browser?
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:
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. Client javascript should be used only for the interfaces.
And there are rumors of an ancient legend about the existence of server javascript, but this is another story. ;)
How to connect to MSSQL by using pure javascript
You can try this (Works only in IE
) :
<!DOCTYPE html>
<html>
<head>
<script>
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";
connection.Open(connectionstring);
//your queries here
rs.close;
connection.close;
</script>
</head>
<body>
</body>
</html>
You really shouldn´t use client side script like javascript to access databases for several reasons like bad practice, security issues etc. You can use .Net, PHP, JAVA etc which are server side language and better way to use these to interact with the databases.
How to connect javascript code to database in ASP.NET
I actually found the solution I was looking for here (suggested by a coworker):
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/
In case the link becomes invalid, the solution is to use Microsoft's Web API 2 with ASP.NET and EntityFramework. By creating Models and controllers in Visual Studio, you can create a context that can be reached with AJAX using routes defined in the RouteConfig access in the JavaScript like so:
var ViewModel = function () {
var self = this;
self.books = ko.observableArray();
self.error = ko.observable();
var booksUri = '/api/books/';
function ajaxHelper(uri, method, data) {
self.error(''); // Clear error message
return $.ajax({
type: method,
url: uri,
dataType: 'json',
contentType: 'application/json',
data: data ? JSON.stringify(data) : null
}).fail(function (jqXHR, textStatus, errorThrown) {
self.error(errorThrown);
});
}
function getAllBooks() {
ajaxHelper(booksUri, 'GET').done(function (data) {
self.books(data);
});
}
// Fetch the initial data.
getAllBooks();
};
How to connect database with browser in node js?
you need to pass the sql query into a nodejs web server then give the result as a respond.
var http = require('http');
var url = require('url');
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "123",
database: "mydb"
});
http.createServer(function (req, res) {
var query = url.parse(request.url, true).query;
res.writeHead(200, {'Content-Type': 'text/html'});
con.connect(function(err) {
if (err) throw err;
var sql = unescape(query.sql);
con.query(sql, function (err, result) {
if (err) throw err;
res.end(JSON.stringify(result));
});
});
}).listen(8080);
Related Topics
How to Invoke a Jsf Managed Bean on a HTML Dom Event Using Native JavaScript
Generate a Hash from String in JavaScript
Tainted Canvases May Not Be Exported
Queryselector and Queryselectorall VS Getelementsbyclassname and Getelementbyid in JavaScript
Using JavaScript to Edit CSS Gradient
Limit Labels Number on Chart.Js Line Chart
CSS Transition Doesn't Work with Top, Bottom, Left, Right
Batch File > JavaScript > Winscp > Check If File Exists
Jquery Selector for Inputs with Square Brackets in the Name Attribute
How to Have a Default Option in Angular.Js Select Box
Chained Promises Not Passing on Rejection
How to Remove Item from Array by Value
Delete Firebase Data Older Than 2 Hours
Converting Between Strings and Arraybuffers
Bootstrap Modal: Background Jumps to Top on Toggle
Full Height of a HTML Element (Div) Including Border, Padding and Margin