4

我在Visual Studio 2015中使用 apache cordovaionic 框架来创建混合应用程序。

我想将我的应用程序连接到云 sql 数据库,例如 azure sql 或 google cloud sql。

我需要能够进行插入、更新和选择。

根据我的研究,我知道我可能必须使用 PHP,但我还没有找到任何好的例子。任何帮助深表感谢。

4

2 回答 2

1

MSDN 概念证明链接 https://msdn.microsoft.com/library/mt715784.aspx

第 1 步:连接 新的连接功能用于连接 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);  
}
于 2016-06-01T04:15:14.297 回答
1

kuhner,Cordova 是一个 Node.js 框架。您当然可以将 Azure SQL 数据库与 Node.js 一起使用。有很多很棒的驱动程序选项可以帮助您开始使用您选择的平台:
- node-mssql
-乏味

这是一个快速的代码示例:

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/en-us/documentation/articles/sql-database-get-started/

于 2016-06-01T16:30:42.517 回答