angularjs – 将Cordova混合应用程序连接到Azure SQL数据库

我在Visual Studio 2015中使用apache cordova和离子框架来创建混合应用程序.

我想将我的应用程序连接到云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);  
}  

第3步:插入一行
在此示例中,您将了解如何安全地执行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);  
}
点赞