angularjs - 将 Cordova 混合应用连接到 Azure SQL 数据库

标签 angularjs cordova ionic-framework azure-sql-database node-mssql

我正在使用 apache Cordova ionic 框架 Visual Studio 2015 创建一个混合应用程序。

我想将我的应用程序连接到 云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 注入(inject)漏洞影响的参数,并检索自动生成的主键值。
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);  
}

关于angularjs - 将 Cordova 混合应用连接到 Azure SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490045/

相关文章:

android - 如何在 android 中使用 SQLitePlugin Cordova 填充数据库?

android - 手机间隙 : Android 6 status bar overlaps webview

html - 在输入下方显示行

css - 将 Ionic Page 分成两个垂直部分,使第二部分可滚动并限制高度

javascript - ng-if 在变量更改时不更新 DOM

javascript - 如何获取复选框的值,其中值在 ng-repeat 中动态变化

android - 如何使用 Cordova 相机插件拍摄多张照片

angular - 为什么 LoadingController 在 API 调用期间尝试关闭它时抛出 'Overlay does not exist '

angularjs - 在 Electron 应用程序中单击菜单更改 AngularJS 状态

unit-testing - Angularjs - 如何用模拟正确替换服务依赖