I’m trying to establish a JavaScript database connection with SQL Server 2005 locally. Can anyone provide sample code showing how to achieve this directly from JavaScript in the browser?
I am currently learning web programming on my desktop, where SQL Server Management Studio 2005 and IE7 are installed. Do I need to use a different scripting language for this, or is there a way to connect directly using JavaScript? If alternatives exist, I’d appreciate suggestions, but my main focus is on JavaScript for now.
The recommended way is to create a backend API that connects to the SQL Server and exposes data to the browser. You can use Node.js with the mssql library:
Install mssql package
npm install mssql
Create a Node.js API (server.js)
const sql = require("mssql");
const express = require("express");
const app = express();
const config = {
user: "your_username",
password: "your_password",
server: "localhost",
database: "your_database",
options: {
encrypt: true, // For Azure SQL
trustServerCertificate: true // For local development
}
};
app.get("/data", async (req, res) => {
try {
await sql.connect(config);
const result = await sql.query("SELECT * FROM your_table");
res.json(result.recordset);
} catch (err) {
res.status(500).send(err.message);
}
});
app.listen(3000, () => console.log(“Server running on port 3000”));
Run the server
sh
Copy
Edit
node server.js
Fetch data from the frontend (HTML + JavaScript)
script
fetch(“http://localhost:3000/data”)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(“Error:”, error));
/script
Use an ODBC Bridge (Only for Intranet Apps)
If this is for an internal application (e.g., an Intranet page running on Internet Explorer 7), you can use ActiveX and ODBC (not recommended for modern applications):
html
Copy
Edit
script
try {
var conn = new ActiveXObject("ADODB.Connection");
var connStr = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=your_database;User Id=your_username;Password=your_password;";
conn.Open(connStr);
var rs = conn.Execute("SELECT * FROM your_table");
while (!rs.EOF) {
console.log(rs.Fields(0).Value);
rs.MoveNext();
}
rs.Close();
conn.Close();
} catch (e) {
console.error("Error:", e.message);
}
/script>
This only works in Internet Explorer and is not safe for modern web applications.
Using IndexedDB for Local Caching & Syncing with SQL Server
If you want some level of database interaction in the browser, but SQL Server is not directly accessible, you can use IndexedDB as a local storage layer and periodically sync with SQL Server via an API.
How It Works
-
Store data locally in IndexedDB when the user performs actions.
-
Sync data with SQL Server using an API when online.
-
Fetch data from SQL Server periodically and update IndexedDB.
Example: Storing and Syncing Data
Frontend (Browser with IndexedDB)
let db;
const request = indexedDB.open("LocalDB", 1);
request.onupgradeneeded = event => {
db = event.target.result;
const store = db.createObjectStore("customers", { keyPath: "id" });
};
request.onsuccess = event => {
db = event.target.result;
};
// Function to save data locally
function saveCustomer(customer) {
const tx = db.transaction("customers", "readwrite");
const store = tx.objectStore("customers");
store.put(customer);
}
// Sync with SQL Server
async function syncWithServer() {
const response = await fetch("/api/sync", { method: "POST" });
const data = await response.json();
const tx = db.transaction("customers", "readwrite");
const store = tx.objectStore("customers");
data.forEach(customer => store.put(customer));
}
Backend (Node.js Express API Syncing with SQL Server)
app.post('/api/sync', async (req, res) => {
let pool = await sql.connect(config);
let result = await pool.request().query("SELECT * FROM Customers");
res.json(result.recordset);
});
Why This Approach?
Works offline—IndexedDB stores data locally and syncs when online.
Fast UI interactions—Reads/writes happen locally before syncing.
Secure—Browser never directly connects to SQL Server.
This is a modern, scalable alternative for web apps that need local persistence before syncing with a central database. 