2011-09-13 118 views
0

我創建了一個使用MS SQL數據庫的c#windows應用程序(2.0框架)。
在開發過程中,我使用了MS VISUAL STUDIO 2010和SQL 2008 MANAGEMENT STUDIO。
開發過程中我的連接字符串是:在C#Windows應用程序中使用MS SQL SERVER 2008 R2 EXPRESS?

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SL;Integrated Security=True"); 

一切工作正常....

現在我想運行客戶端系統上這個應用程序。
因此,在客戶端系統上成功安裝了MS SQL SERVER 2008 EXPRESS。
停止我的系統的sql服務,並從我的機器上覆制.mdf和.ldf文件並粘貼到客戶端的「c:\ Database \」中。 但運行時出現問題。

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)

我已經試過很多次的連接字符串是:

SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True"); 

SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass"); 

SqlConnection con = new SqlConnection("Data Source=.\\MSSQLEXPRESS; AttachDbFilename =C:\\Database\\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); 

當我使用的連接字符串User ID=sa;Password=pass我得到:

authentication failed for 'sa'

我錯過了一些步驟,或做錯誤?請告訴我從系統複製數據庫後應該做什麼。我應該在C#中使用連接字符串?

謝謝!

回答

2

您只複製了將數據庫附加到SQLExpress所需的文件,查看使用OSQL,或者將客戶端工具安裝到具有SQLExpress並附加數據庫的PC上。

0

你確定新的實例名是'MSSQLEXPRESS'嗎?

我的R2 Express實例被稱爲'SQLEXPRESS'(這實際上是由於installer中的已知錯誤)。

您可能想要檢查實例是否被實際稱爲您的想法。

+0

是...即MSSQLEXPRESS。 –

1

首先,一定要檢查出Sres' answer
如果你沒有告訴連接字符串中的SQL Server連接你的數據庫(使用AttachDbFilename,就像在第三個例子中一樣),你必須按照他的說法去做自己的附件。

關於您的三個連接字符串示例:它們都只在某些情況下才起作用。你可能想看看connectionstrings.com

這裏有你的三個連接字符串,加上他們的問題簡短解釋:

Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Integrated Security=True

- >這將使用您的應用程序在其下運行當前的Windows用戶。所以當前的Windows用戶必須擁有客戶機器上數據庫的權限。

Data Source=.\MSSQLEXPRESS;Initial Catalog=SL;Persist Security Info=True;User ID=sa;Password=pass

- >這裏使用了特殊的 '山' 的用戶帳戶。爲了這個工作,你必須確保滿足以下先決條件:

  • Mixed mode authentication必須設置(如果你不這樣做,你只擁有Windows認證,和「山」是一個SQL Server認證用戶名)
  • 'sa'的密碼需要指定,當然它必須與開發機器上的相同。

但是,這不是最好的解決方案。 'sa'是具有完全權限的管理員帳戶,並且您不應使用管理員帳戶通過您的應用訪問SQL Server。
如果您確實想使用SQL Server身份驗證(而不是Windows身份驗證),最好使用您的應用程序所需的最小必需權限創建一個新帳戶。

Data Source=.\MSSQLEXPRESS; AttachDbFilename =C:\Database\SL.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True

- >相同的問題與第一個連接字符串(當前Windows用戶必須擁有權限)。 Plus,User Instance=True需要在SQL Server中啓用。從connectionstrings.com
報價:安裝時我提供了這個名字

To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.

相關問題