. Một máy chủ web mini vào máy chủ cơ sở dữ liệu, vì vậy câu trả lời này vẫn là một tuyến đường khác bạn có thể đi.]
Bạn cũng có thể kết nối trực tiếp bằng ổ cắm [Google "ổ cắm JavaScript"] và trực tiếp tại thời điểm này, tôi có nghĩa là sử dụng tệp flash cho mục đích này, mặc dù HTML5 có ổ cắm web như một phần của thông số kỹ thuật mà tôi tin rằng cho phép bạn làm điều tương tự.
Một số người trích dẫn các vấn đề bảo mật, nhưng nếu bạn thiết kế chính xác các quyền cơ sở dữ liệu của mình, bạn nên về mặt lý thuyết, bạn có thể truy cập cơ sở dữ liệu từ bất kỳ mặt trước nào, bao gồm OSQL và không vi phạm bảo mật. Sau đó, vấn đề bảo mật sẽ là nếu bạn không kết nối qua SSL.
Cuối cùng, mặc dù, tôi khá chắc chắn rằng đây là tất cả các lý thuyết vì tôi không tin bất kỳ thư viện JavaScript nào tồn tại để xử lý các giao thức truyền thông cho SSL hoặc SQL Server, vì vậy trừ khi bạn sẵn sàng tìm ra những điều này Tốt hơn là đi theo tuyến đường có một ngôn ngữ tập lệnh phía máy chủ và phía máy chủ ở giữa trình duyệt và cơ sở dữ liệu.
Bỏ qua nội dung chính
Trình duyệt này không còn được hỗ trợ.
Nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, cập nhật bảo mật và hỗ trợ kỹ thuật.
Bước 3: Bằng chứng về khái niệm kết nối với SQL bằng Node.js
- Bài báo
- 09/09/2022
- 2 phút để đọc
Trong bài viết này
Tải xuống trình điều khiển SQL Node.js
Ví dụ này nên được coi là một bằng chứng của khái niệm. Mã mẫu được đơn giản hóa cho rõ ràng và không nhất thiết phải đại diện cho các thực tiễn tốt nhất được đề xuất bởi Microsoft. Các ví dụ khác, sử dụng các chức năng quan trọng tương tự có sẵn trên GitHub:
- //github.com/tediousjs/tedious/blob/master/examples/
Bước 1: Kết nối
Hàm kết nối mới được sử dụng để kết nối với cơ sở dữ liệu SQL.new Connection function is used to connect to SQL Database.
var Connection = require['tedious'].Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection[config];
connection.on['connect', function[err] {
// If no error, then good to proceed.
console.log["Connected"];
}];
connection.connect[];
Bước 2: Thực hiện truy vấn
Tất cả các câu lệnh SQL được thực thi bằng hàm request [] mới. Nếu câu lệnh trả về các hàng, chẳng hạn như câu lệnh select, bạn có thể truy xuất chúng bằng hàm request.on []. Nếu không có hàng, hàm request.on [] trả về danh sách trống.new Request[] function. If the statement returns rows, such as a select statement, you can retrieve them using the request.on[] function. If there are no rows, the request.on[] function returns empty lists.
var Connection = require['tedious'].Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection[config];
connection.on['connect', function[err] {
// If no error, then good to proceed.
console.log["Connected"];
executeStatement[];
}];
connection.connect[];
var Request = require['tedious'].Request;
var TYPES = require['tedious'].TYPES;
function executeStatement[] {
request = new Request["SELECT c.CustomerID, c.CompanyName,COUNT[soh.SalesOrderID] AS OrderCount FROM SalesLT.Customer AS c LEFT OUTER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID GROUP BY c.CustomerID, c.CompanyName ORDER BY OrderCount DESC;", function[err] {
if [err] {
console.log[err];}
}];
var result = "";
request.on['row', function[columns] {
columns.forEach[function[column] {
if [column.value === null] {
console.log['NULL'];
} else {
result+= column.value + " ";
}
}];
console.log[result];
result ="";
}];
request.on['done', function[rowCount, more] {
console.log[rowCount + ' rows returned'];
}];
// Close the connection after the final event emitted by the request, after the callback passes
request.on["requestCompleted", function [rowCount, more] {
connection.close[];
}];
connection.execSql[request];
}
Bước 3: Chèn một hàng
Trong ví dụ này, bạn sẽ thấy cách thực hiện một câu lệnh chèn một cách an toàn, truyền các tham số, bảo vệ ứng dụng của bạn khỏi các giá trị tiêm SQL.
var Connection = require['tedious'].Connection;
var config = {
server: 'your_server.database.windows.net', //update me
authentication: {
type: 'default',
options: {
userName: 'your_username', //update me
password: 'your_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'your_database' //update me
}
};
var connection = new Connection[config];
connection.on['connect', function[err] {
// If no error, then good to proceed.
console.log["Connected"];
executeStatement1[];
}];
connection.connect[];
var Request = require['tedious'].Request
var TYPES = require['tedious'].TYPES;
function executeStatement1[] {
request = new Request["INSERT SalesLT.Product [Name, ProductNumber, StandardCost, ListPrice, SellStartDate] OUTPUT INSERTED.ProductID VALUES [@Name, @Number, @Cost, @Price, CURRENT_TIMESTAMP];", function[err] {
if [err] {
console.log[err];}
}];
request.addParameter['Name', TYPES.NVarChar,'SQL Server Express 2014'];
request.addParameter['Number', TYPES.NVarChar , 'SQLEXPRESS2014'];
request.addParameter['Cost', TYPES.Int, 11];
request.addParameter['Price', TYPES.Int,11];
request.on['row', function[columns] {
columns.forEach[function[column] {
if [column.value === null] {
console.log['NULL'];
} else {
console.log["Product id of inserted item is " + column.value];
}
}];
}];
// Close the connection after the final event emitted by the request, after the callback passes
request.on["requestCompleted", function [rowCount, more] {
connection.close[];
}];
connection.execSql[request];
}
Nhận xét
Gửi và xem phản hồi cho