Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Contoh ini harus dianggap sebagai bukti konsep saja. Kode sampel disederhanakan untuk kejelasan, dan tidak selalu mewakili praktik terbaik yang direkomendasikan oleh Microsoft. Contoh lain, yang menggunakan fungsi penting yang sama tersedia di repositori sampel GitHub.
Prasyarat
Sebelum Anda menjalankan kode sampel, pastikan prasyarat berikut terpenuhi:
- Node.js diinstal pada komputer pengembangan Anda. Unduh dari nodejs.org.
- Paket npm yang melelahkan diinstal. Jalankan
npm install tediousdi direktori proyek Anda. - Instans SQL Server tersedia dan dikonfigurasi untuk menerima koneksi:
- Protokol TCP/IP diaktifkan di SQL Server Configuration Manager. TCP/IP dinonaktifkan secara default pada edisi SQL Server Express.
- Layanan Browser SQL Server berjalan jika Anda tersambung ke instans bernama.
- Firewall memungkinkan koneksi pada port SQL Server (default: 1433).
- Untuk autentikasi SQL Server, login SQL diaktifkan dan SQL Server dikonfigurasi untuk autentikasi mode campuran.
Petunjuk / Saran
Jika Anda menerima kesalahan koneksi seperti Failed to connect, verifikasi bahwa TCP/IP diaktifkan dan layanan SQL Server sedang berjalan. Untuk pengembangan lokal, coba sambungkan dengan server: 'localhost' atau server: '127.0.0.1'.
Langkah 1: Sambungkan
Gunakan fungsi Koneksi baru untuk menyambungkan ke 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're 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();
Langkah 2: Menjalankan kueri
Jalankan semua pernyataan SQL menggunakan fungsi Permintaan baru . Jika pernyataan mengembalikan baris, seperti pernyataan pilih, Anda dapat mengambilnya menggunakan fungsi request.on . Jika tidak ada baris, fungsi request.on mengembalikan daftar kosong.
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're 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() {
var 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);
}
Langkah 3: Sisipkan baris
Dalam contoh ini, Anda akan melihat cara menjalankan pernyataan INSERT dengan aman, melewati parameter, yang melindungi aplikasi Anda dari nilai injeksi 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're 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() {
var 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);
}
Langkah 4: Sambungkan dengan autentikasi Windows
Driver yang melelahkan mendukung autentikasi Windows menggunakan NTLM. Untuk menyambungkan dengan kredensial domain alih-alih autentikasi SQL Server, ubah authentication bagian dalam konfigurasi koneksi:
var Connection = require('tedious').Connection;
var config = {
server: '<server>', //update me
authentication: {
type: 'ntlm',
options: {
domain: '<domain>', //update me
userName: '<username>', //update me
password: '<password>' //update me
}
},
options: {
encrypt: false,
database: '<database>', //update me
port: 1433
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log('Connection failed', err);
} else {
console.log('Connected with Windows authentication');
}
});
connection.connect();
Nota
Autentikasi NTLM mengharuskan Anda untuk memberikan kredensial domain dalam konfigurasi. Untuk koneksi tepercaya yang menggunakan pengguna Windows yang saat ini masuk tanpa menentukan kredensial, pertimbangkan paket msnodesqlv8 , yang menggunakan driver ODBC asli.