1

我有一個SQL Server和開發都在本地工作的項目。遠程SQL Server連接字符串

下面的連接字符串中使用:

SqlConnection connection= new SqlConnection("Data Source=.\\MYDB;Initial Catalog=Database;Integrated Security=true"); 
    connection.Open(); 

我的開發團隊正在成長,我期待有另一個開發者訪問這個相同的數據庫。

他將需要改變這種連接字符串,我們正在努力鍛鍊什麼的字符串改變 - 到目前爲止,我們已經嘗試:

SqlConnection connection= new SqlConnection("Data Source=2.221.1.145\\MYDB;Initial Catalog=Database;Integrated Security=true"); 
    connection.Open(); 

但被訪問頁面時拋出了以下錯誤:

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352431 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244 
    System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5363103 
    System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145 
    System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +922 
    System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307 
    System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +518 
    System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +278 
    System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38 

SQL Server已啓用TCPIP協議並偵聽我係統上的所有IPAddress。端口1433被添加到防火牆,用於UDP和TCP入站,並且SQL Server Express實例服務已重新啓動。

任何人都可以請幫助我們的連接字符串?另外我覺得集成安全線將需要改變。

爲了簡單起見,我提出的問題與更新的狀態,以一個新的線程:Connection string for SQL Server Express on remote Computer login failed error

+0

如果我在連接時遇到問題,我經常使用IP地址。但是...你是否嘗試過DNS服務器的名稱? – DMason

+0

是的 - 我得到同樣的問題! – RenegadeAndy

+0

你正在運行什麼版本的SQL?它是「localDB」版本嗎? 'SELECT @@ VERSION'會很容易地告訴你。 – DMason

回答

0

MYDBTry連接字符串是這樣的:

SqlConnection connection= new SqlConnection("Server=2.221.1.145;Database=Database;Trusted_Connection=True"); 

(分出來的服務器和數據庫參數需要)

另外,你提到你打開的端口是好的。還有一部分是你應該看看的SQL配置。進入SQL服務器上的SQL配置管理器。展開SQL Server網絡配置。 {yourinstance}的開放協議,然後雙擊TCP/IP。在IP地址標籤下,滾動到最下面。確保TCP動態端口爲空。如您所期望的那樣確認TCP端口是1433。

EDIT1: 根據評論建議更新了連接。

此外 - 由於您正在遠程連接,請仔細查看Trusted_Connection位。您在本地計算機上運行的Windows帳戶是否能夠登錄並連接到遠程服務器上的SQL?如果你沒有做任何遠程的事情,而且它在同一個子網中,你可能不需要擔心這一點。

EDIT2:

安迪 - 根據您的評論您的問題似乎是,你的連接不被信任,你將需要輸入用戶名和密碼。如果您使用的是SQL登錄,那麼這是在SQL中定義的。如果您使用Windows身份驗證,則需要將您的域用戶帳戶添加到允許的登錄名中。

如果您正在使用SQL登錄,則需要使用「標準安全」樣式連接字符串,而如果您使用的是域帳戶,則需要使用「可信連接」樣式連接字符串。見這裏的一個例子的字符串:

https://www.connectionstrings.com/sql-server-2012/

標準安全 服務器= myServerAddress;數據庫= MyDatabase的;用戶ID =名爲myUsername; Password = myPassword;

可信連接 Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;

現在,您可能會問如何設置此SQL登錄!您將需要創建一個SQL登錄名和一個數據庫用戶。這可以從SQL Management Studio中一步完成。這裏是一個詳細的如何,從MSDN:

http://msdn.microsoft.com/en-us/library/aa337562.aspx

+0

我認爲OP正試圖連接到一個命名實例 - 在本例中是「MYDB」。 – DMason

+0

很好,謝謝! –

+0

根據OP提供的內容,「MYDB」是SQL Server實例的名稱,而不是數據庫的名稱。如果他有正確的IP地址,那麼他的連接字符串可能沒有問題。 – DMason

1

不是100%肯定,如果它導致的錯誤,但我已經看到了這行的人了,當遠程連接到SQL Server的第一次,因此值得檢查:

打開SSMS中的服務器屬性,在連接下,檢查是否檢查Allow remote connections to this server。即使在同一個局域網上,您也需要檢查它以連接另一臺計算機。

+0

這是檢查:) - 但不能解決問題。 – RenegadeAndy

0

檢查「Integrated Security ='true'」是否是您的問題。

如果是這樣,這意味着您的遠程連接將不得不提供訪問/安全證書。