1

I'm trying to connect to Microsoft SQL Server using Google Apps Script. I'm using SQL Server 2008 R2 and I am using one of the suggested scripts that's supposed to read data and put it into a Spreadsheet: https://developers.google.com/apps-script/jdbc#reading_from_a_database

The error message is:

Failed to establish a database connection. Check connection string, username, and password

Username and password are OK, the user is DBowner. The port is also correct, I tried to connect to the server via Telnet using: o IP-address 1433 and it works.

Here's the code:

function foo() {
    var conn = Jdbc.getConnection("jdbc:sqlserver://IP-adress:1433/DBName","user","password");
    var stmt = conn.createStatement();
    stmt.setMaxRows(100);
    var start = new Date();
    var rs = stmt.executeQuery("select * from person");
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var cell = doc.getRange('a1');
    var row = 0;
    while (rs.next()) {
        for (var col = 0; col < rs.getMetaData().getColumnCount(); col++) {
            cell.offset(row, col).setValue(rs.getString(col + 1));
        }
        row++;
    }
    rs.close();
    stmt.close();
    conn.close();
    var end = new Date();
    Logger.log("time took: " + (end.getTime() - start.getTime()));
}

Do you have any idea of what can be wrong? Do I have to make some configuration on my Server? Or in the database? The instructions mentioned above says to ensure that Google's Ip-addresses can reach the database. But instead of listing all of Google's IP-addresses I granted access to all on that port. I also enabled TCP/IP Protocol in SQL Server Configuration Manager. And I granted "Remote connections" to the Server in MSSMS. Any other idea, please?

4

2 回答 2

3

好吧,我在这里找到了答案: Google Apps Scripts/JDBC/MySQL

显然,连接字符串必须如下所示:

var conn = Jdbc.getConnection("jdbc:sqlserver://IP-address:1433;" + "databaseName=DBName;user=username;password=password;");

我不明白为什么连接字符串与谷歌文档中的不同,但这个对我有用......

于 2013-09-24T21:20:40.423 回答
0

问题在连接字符串中。

应该是这样的

          address = '%YOUR SQL HOSTNAME%';
          user = '%YOUR USE%';
          userPwd = '%YOUR PW%';
          dbUrl = 'jdbc:sqlserver://' + address + ':1433;databaseName=' + queryDb;
          var conn = Jdbc.getConnection(dbUrl, user, userPwd);

我在 GitHub 上有一个完整的工具,您可以在其中连接到 MySQL 和 SQL Server。我将能够使用它来帮助您。我会不断更新更多功能加班!你可以在这里找到它。

GOOGLE 电子表格 JDBC 连接器工具

于 2017-06-26T20:48:23.550 回答