我正在使用 Visual Studio 2015 中的 Apache Cordova 和 Ionic Framework 创建混合应用程序。
我想将我的应用程序连接到云 SQL 数据库,例如 Azure SQL 或 Google Cloud SQL。
我需要能够执行插入、更新和选择操作。
根据我的研究,我了解到可能需要使用 PHP,但我没有找到任何好的示例可供参考。非常感谢您的帮助。
我正在使用 Visual Studio 2015 中的 Apache Cordova 和 Ionic Framework 创建混合应用程序。
我想将我的应用程序连接到云 SQL 数据库,例如 Azure SQL 或 Google Cloud SQL。
我需要能够执行插入、更新和选择操作。
根据我的研究,我了解到可能需要使用 PHP,但我没有找到任何好的示例可供参考。非常感谢您的帮助。
MSDN概念验证链接 https://msdn.microsoft.com/library/mt715784.aspx
步骤1:连接 使用新的Connection函数连接到SQL数据库。 JavaScript
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver.database.windows.net',
// If you are on Microsoft Azure, you need this:
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
步骤2:执行查询 所有的SQL语句都是使用新的Request()函数执行的。如果语句返回行,比如select语句,您可以使用request.on()函数检索它们。如果没有行,request.on()函数返回空列表。 JavaScript
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver.database.windows.net',
// When you connect to Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement();
});
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');
});
connection.execSql(request);
}
步骤三:插入行 在这个例子中,您将看到如何安全地执行INSERT语句,传递参数以保护您的应用程序免受SQL注入漏洞,并检索自动生成的主键值。 JavaScript
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver.database.windows.net',
// If you are on Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement1();
});
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);
}
});
});
connection.execSql(request);
}
您好,Cordova是一个基于Node.js的框架。您可以使用Azure SQL数据库与Node.js进行开发。以下是一些优秀的驱动程序选项,可帮助您在所选平台上快速入门:
- node-mssql
- tedious
这里有一个快速的代码示例:
var Connection = require('tedious').Connection;
var config = {
userName: 'yourusername',
password: 'yourpassword',
server: 'yourserver.database.windows.net',
// When you connect to Azure SQL Database, you need these next options.
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
executeStatement();
});
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');
});
connection.execSql(request);
}
要开始使用Azure SQL DB,请按照以下链接:https://azure.microsoft.com/zh-cn/documentation/articles/sql-database-get-started/