簡體   English   中英

遠程SQL Server的ConnectionString

[英]ConnectionString for remote SQL Server

就是這種情況,我想連接到位於遠程服務器上的SQL2008R2,我可以遠程進入服務器,並且在Windows上以管理員身份登錄后,我在SQL Server MS上工作(與SQL的連接是通過Windows身份認證)。

我正在開發需要與SQL連接的WinForms應用程序,我嘗試了幾種connectionString都沒有成功,例如:

public static string strCon = @"Server=190.xxx.xxx.xxx\ServerName,1433;Database=DataBaseName;Integrated Security=false;User ID=User;Password=Pass";

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName,1433;Network Library=DBMSSOCN;Initial Catalog=DataBaseName;User ID=User;Password=Pass;

我想念什么嗎? 在嘗試連接之前,我還應該做其他事情嗎?

我認為我對Windows身份驗證一無所知,我可以繞過它直接連接到SQL嗎?

請注意,在服務器上啟用了遠程連接,我檢查了端口為1433。

編輯:

在尋找解決方案時,我發現了這個問題 ,我意識到當我遠程連接到服務器時,我會使用端口(190.xxx.xxx.xxx:yyyy)。 我的問題就在那里,不是嗎?

EDIT2:我的解決方案:我使用的connectionString很好,問題是端口。 我為我的辦公室IP打開了它,一切都像個魅力。

我強烈建議您使用SqlConnectionStringBuilder類,該類允許您通過屬性配置連接字符串,並保證創建了有效的連接字符串。

像這樣使用它:

SqlConnectionStringBuilder b = new SqlConnectionStringBuilder();
b.DataSource = "190.xxx.xxx.xxx\ServerName";
b.InitialCatalog = "DataBaseName";
b.IntegratedSecurity = false;
b.UserId = "...";
b.Password = "...";

string connectionString = b.ConnectionString;

如果要使用Windows身份驗證進行連接,則應使用:

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName,1433;Database=DataBaseName;Integrated Security=True";

另請注意,1433是默認的sql端口,因此您不必指定它。 您可以這樣寫:

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName;Database=DataBaseName;Integrated Security=True";

在這種情況下,不應指定User NamePassword因為它將使用您當前會話的Windows帳戶。

您可以在此處找到有關SQL Server連接字符串的更多詳細信息

如果要使用用戶標識和密碼進行連接,則忘記了IntegratedSecurity屬性

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName,1433;Network Library=DBMSSOCN;Initial Catalog=DataBaseName;Integrated Security=False;User ID=User;Password=Pass;

除此以外:

public static string strCon = @"Data Source=190.xxx.xxx.xxx\ServerName,1433;Network Library=DBMSSOCN;Initial Catalog=DataBaseName;Integrated Security=True;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM