Can A Mobile Service Server Script (schedule) Access Other Sql Azure Databases?
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 coul
Solution 1:
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.
Post a Comment for "Can A Mobile Service Server Script (schedule) Access Other Sql Azure Databases?"