我創建了一個將連接到SQL Server以運行存儲過程的類。在Windows窗體解決方案中使用此類時,可以成功訪問數據庫。當班級放入Windows服務時,出現以下錯誤:從Windows服務通過網絡訪問SQL Server
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 conn = new SqlConnection(j.ConnectionString);
SqlCommand cmd = new SqlCommand(j.Query, conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("Table1");
da.Fill(ds, "Table1"); // <----- error occurs here (Windows Service only)
的SQL Server版本是
Microsoft SQL Server 2008 (SP1) - 10.0.2766.0 (X64)
Enterprise Edition (64-bit) on Windows NT 5.2 <X64>
(Build 3790: Service Pack 2)
在Visual Studio版本是Microsoft Visual C# 2008
Framework版本是3.5 SP1
。
我會查看服務運行的用戶。它是一個本地用戶嗎? – 2010-07-01 14:16:58
你使用什麼連接字符串,你使用集成或sql服務器身份驗證? – 2010-07-01 14:21:55
我試過使用「本地服務」,「網絡服務」,「本地系統」和「用戶」。 連接字符串是「Persist Security Info = False; User ID = call_tracker; Password = pCa11Tracker; Initial Catalog = CallData; Data Source = SQLData \ CORP」,它與用於Windows窗體應用程序的連接字符串完全相同工作(從配置文件中讀取字符串)。 – flynn 2010-07-01 15:36:46