可以在移动服务服务器脚本(附表)访问其它SQL Azure数据库? [英] Can a mobile service server script (schedule) access other Sql Azure databases?

查看:92
本文介绍了可以在移动服务服务器脚本(附表)访问其它SQL Azure数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待使用的Azure移动服务创建计划任务。

由于该服务将最终调用另一个云服务(网站),我想知道如果移动脚本可以访问数据库云服务已经这样做。

我知道你可以指定一个数据库来使用移动脚本(我选择了免费的记录),但似乎无法判断是否可以通过的 API

  VAR todoItemsTable = tables.getTable('TodoItems');

...打趣

  VAR todoItemsTable = databases.getDatabase('NonMobileSqlDb')tables.getTable('TodoItems')。

我已经检查<一href=\"http://stackoverflow.com/questions/14026459/can-you-mix-azure-mobile-services-with-azure-cloud-services\">this问题(你能与Azure的云服务Azure的混合移动服务?),但它似乎没有涵盖的脚本说话数据库。

一些背景...

移动业务将(按计划)调用Web服务(使用授权)执行日常操作。我想锁定此服务(无SSL)和一种方法是生成密钥的服务可以使用的云服务可以验证。此密钥将被存储在数据库中都可以访问和只对很短的时间周期可


解决方案

当然可以。

您需要使用从的 Node.js的)连接=HTTP ://www.windowsazure.com/en-us/develop/nodejs/how-to-guides/sql-database/相对=nofollow>如何引导:


  

要使用节点SQLSERVER,你必须要求它在你的应用程序,
  指定一个连接字符串。连接字符串应该是ODBC
  值在如何返回:获取SQL数据库连接信息
  这篇文章的部分。在code应该出现类似
  以下内容:


  VAR SQL =要求('节点SQLSERVER');
VAR conn_str =驱动程序= {SQL服务器本机客户端10.0};Server=tcp:{dbservername}.database.windows.net,1433;Database={database};Uid={username};Pwd={password};Encrypt=yes;Connection超时= 30;;


  

查询可以通过指定使用Transact-SQL语句执行
  查询方法。下面code创建一个HTTP服务器和
  从ID,列1和列2行中的测试表返回数据
  当您查看网页:


  VAR HTTP =要求(HTTP)
VAR端口= process.env.port || 3000;
http.createServer(功能(REQ,RES){
    sql.query(conn_str,SELECT * FROM TestTable的功能(错了,结果){
        如果(ERR){
            res.writeHead(500,{'的Content-Type:text / plain的'});
            res.write(遇到错误:-(+ ERR);
            重发();
            返回;
        }
        res.writeHead(200,{'的Content-Type:text / plain的'});
        对于(VAR I = 0; I&LT; results.length;我++){
            res.write(ID:+结果[I] .ID +列1:+结果[I] .Column1 +列2:+结果[I] .Column2);
        }
        res.end(完成);
    });
})听(端口);

非常感谢@GauravMantri&安培; @hhaggan他们在得到这个地步帮助。

I'm looking to create a scheduled job using a Azure mobile service.

Since the service will end up calling another cloud service (website), I was wondering if the mobile script could access a database the cloud service already does.

I understand you can specify a database to use for the mobile script (I selected free for logging) but can't seem to tell if you can access other databases through the API.

var todoItemsTable = tables.getTable('TodoItems');

Hypothetically...

var todoItemsTable = databases.getDatabase('NonMobileSqlDb').tables.getTable('TodoItems');

I've already checked this question (Can you mix Azure Mobile Services with Azure Cloud Services?) but it doesn't seem to cover scripts talking to databases.

Some background...

The mobile service will (on a schedule) invoke a web service (with authorisation) that performs routine actions. I'd like to lock down this service (without ssl) and one way is to generate a key the service could use that the cloud service could verify. This key would be stored in the database both can access and only be available for a short period of time.

解决方案

Yes you can.

You need to connect using the following example (uses Node.js) taken from the how-to guide:

To use the node-sqlserver, you must require it in your application and specify a connection string. The connection string should be the ODBC value returned in the How to: Get SQL Database connection information section of this article. The code should appear similar to the following:

var sql = require('node-sqlserver');
var conn_str = "Driver={SQL Server Native Client 10.0};Server=tcp:{dbservername}.database.windows.net,1433;Database={database};Uid={username};Pwd={password};Encrypt=yes;Connection Timeout=30;";

Queries can be performed by specifying a Transact-SQL statement with the query method. The following code creates an HTTP server and returns data from the ID, Column1, and Column2 rows in the Test table when you view the web page:

var http = require('http')
var port = process.env.port||3000;
http.createServer(function (req, res) {
    sql.query(conn_str, "SELECT * FROM TestTable", function (err, results) {
        if (err) {
            res.writeHead(500, { 'Content-Type': 'text/plain' });
            res.write("Got error :-( " + err);
            res.end("");
            return;
        }
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        for (var i = 0; i < results.length; i++) {
            res.write("ID: " + results[i].ID + " Column1: " + results[i].Column1 + " Column2: " + results[i].Column2);
        }
        res.end("; Done.");
    });
}).listen(port);

Many thanks to @GauravMantri & @hhaggan for their help in getting this far.

这篇关于可以在移动服务服务器脚本(附表)访问其它SQL Azure数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆