3

我試圖使用Google App腳本將Google表單連接到Azure上託管的SQL Server實例(DBaaS)。我不斷收到一條錯誤消息,指出我的連接字符串無效,儘管我多次嘗試修改代碼。我可以使用本地計算機上的Microsoft SQL Server Management Studio和HeidiSQL連接到Azure上的SQL Server實例。請注意,我列出了每個IP地址(0.0.0.0到255.255.255.255)以確保它不是阻止我連接的防火牆問題。將Google App腳本連接到Azure上的SQL Server(DBaaS)

//var conn = Jdbc.getConnection('jdbc:sqlserver:MyDBName.database.windows.net:1433/MyDBName', 'MyDBUserName', 'MyDBPassword'); 

    // SECOND ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net:1433;'+ 'databaseName=MyDBName;user=MyDBUserName;password=MyDBPassword;'); 

    // THIRD ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net/MyDBName:1433', 'MyDBUserName', 'MyDBPassword'); 

    // FOURTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('MyDBName.database.windows.net','MyDBUserName', 'MyDBPassword'); 

    // FIFTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net', {user:'MyDBUserName', password:'MyDBPassword'}); 

    // SIXTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('MyDBName.database.windows.net', {user:'MyDBUserName', password:'MyDBPassword'}); 

    // SEVENTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net:1433/MyDBName', {user:'MyDBUserName', password:'MyDBPassword'}); 

    // EIGHT ITERATION OF CONNECTION STRING 
    //https://developers.google.com/apps-script/reference/jdbc/jdbc 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net/MyDBName:1433', {user:'MyDBUserName', password:'MyDBPassword'});  

    // NINTH ITERATION OF CONNECTION STRING - Now I'm just throwing anything at the wall and seeing what sticks! 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net/MyDBName;user=MyDBUserName;password=MyDBPassword'); 

    // TENTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:mysql://MyDBName.database.windows.net/MyDBName:1433', {user:'MyDBUserName', password:'MyDBPassword'});  

    // ELEVENTH ITERATION OF CONNECTION STRING 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net:1433;databaseName=MyDBName','MyDBUserName','MyDBPassword'); 

    //TWELVTH 
    //var conn = jdbc:sqlserver://MyDBName.database.windows.net;database=MyDBName;user=MyDBUserName;password=MyDBPassword; 

    // THIRTEENTH 
    //var conn = Jdbc.getConnection('jdbc:sqlserver://MyDBName.database.windows.net;user=MyDBUserName;password=MyDBPassword;databaseName=MyDBName;'); 

    // FOURTEENTH 
    //var conn = Jdbc.getConnection("jdbc:sqlserver//MyDBName.database.windows.net:1433;databaseName=MyDBName;user=MyDBUserName;password=MyDBPassword"); 

    // FIFTEENTH 
    //var conn = Jdbc.getConnection("jdbc:sqlserver://MyDBName.database.windows.net:1433;databaseName=MyDBName","MyDBUserName","MyDBPassword"); 

    // SIXTEENTH 
    // http://stackoverflow.com/questions/18978380/error-when-connecting-to-mssql-server-with-google-apps-script-via-jdbc?rq=1 
    //var conn = Jdbc.getConnection("jdbc:sqlserver://NumericalIPAddress:1433;" + "databaseName=MyDBName;user=MyDBUserName;password=MyDBPassword;"); 

    // SEVENTEENTH 
    // same as above with one less semicolon 
    //var conn = Jdbc.getConnection("jdbc:sqlserver://NumericalIPAddress:1433;" + "databaseName=MyDBName;user=MyDBUserName;password=MyDBPassword"); 

    //EIGHTEENTH 
    // http://stackoverflow.com/questions/15440939/querying-sql-server-with-google-apps-script-via-jdbc 
    var conn = Jdbc.getConnection("jdbc:sqlserver://MyDBName.database.windows.net:1433;databaseName=MyDBName","MyDBUserName","MyDBPassword"); 

回答

3

您可以在門戶中找到應該用於數據庫的確切連接字符串。點擊當前門戶網站(https://manage.windowsazure.com)中的數據庫,您應該看到一個標題爲「連接到您的數據庫」的部分,下面有一個鏈接指出「查看ADO.Net,ODBC,PHP和JDBC的SQL數據庫連接字符串。

另外,如果您使用的是Azure的門戶網站(https://portal.azure.com)的新版本中,您可以通過瀏覽找到連接字符串全部> SQL數據庫> MyDBName>顯示數據庫連接字符串。

的例子提供對我來說,看起來像: jdbc:sqlserver://server21.database.windows.net:1433; database = Test; user = myuser @ server21; password = {your_password_here}; encrypt = true; hostNameInCertificate = *。database.windows。 net; loginTimeout = 30;

+2

或者,如果您正在使用新版本的Azure門戶([https://portal.azure.com](https://portal.azure.com)),則可以找到連接字符串通過瀏覽所有> SQL數據庫> MyDBName>顯示數據庫連接字符串。 – tmullaney

+1

真棒,我直接添加到答案! – cbattlegear

+0

謝謝@cbattlegear –

1

我試圖連接到Azure數據庫,發現在Azure門戶中指定的JDBC連接字符串和Google 的示例不起作用原樣。 Azure包含的腳本引擎所說的一些屬性不受支持,而Google(從MySQL更新)不起作用。這是我得到了什麼工作:

var user = '[email protected]'; 
var userPwd = 'PASSWORD'; 
var database = 'DB_NAME' 
var connectionString = 'jdbc:sqlserver://SERVER.database.windows.net:1433;databaseName=' + database; 

var conn = Jdbc.getConnection(connectionString , user, userPwd); 

注:「數據庫=」不支持,但「的databaseName =」支持。沒有支持加密或證書標籤。

此外,還有一大組IP範圍必須添加到服務器實例的防火牆規則中。如果您使用門戶添加這些注意事項,您一次只能對防火牆規則進行一次更改;這意味着添加一個範圍,保存,重複。不要添加全部10個,然後在嘗試保存時出錯(就像我第一次那樣)。請參閱:https://developers.google.com/apps-script/guides/jdbc