我嘗試手動連接到我的網絡上的SQL服務器時出現問題。通過SqlClient的Sql連接字符串
我有一個可以正常工作的conn字符串的web API,我有一個VS2015 winforms項目,我設置了使用VS工具連接測試conn,這也可以正常工作。
但是,當我嘗試通過SQLconnection連接失敗,並說服務器無法找到或不能訪問。
因此,繼承人的測試代碼...
string connectionString = @"Data Source=test1\test1;Initial Catalog=my_test;Integrated Security=True;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
// Create the Command and Parameter objects.
SqlCommand command = new SqlCommand("", connection);
// Open the connection in a try/catch block.
// Create and execute the DataReader, writing the result
// set to the console window.
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}\t{2}",
reader[0], reader[1], reader[2]);
}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
現在我真的不關心結果,或者什麼,我只需要建立連接。我無法弄清楚爲什麼會拋出這個錯誤,當我的Web API工作,並且連接到數據庫的VS工具也能正常工作。
我已經檢查了服務器並啓用了命名管道,只是爲了確保。
任何想法?
編輯:我捕獲錯誤...
{"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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"}
約15秒試圖打開連接後會發生。 事情是這個connectiin字符串我試着是完全相同的字符串通過SQL工具使用,我這樣做,所以我知道我有一個有效的字符串。
ODBC連接也使用相同的消息。
我從我的機械檢查是我可以聽港口1433,但它說服務器沒有聽。 然而,看看服務器設置爲動態端口與任何範圍的動態端口41934,所以我不知道爲什麼它說它不聽evenb雖然它動態端口
EDIT2:嗯,必須是權限或司機...只是嘗試完全相同的代碼,並立即工作...
這是潛入我未知的境界。如果找不到解決
也許運行WinForm應用程序的用戶無法訪問數據庫?嘗試以可以訪問數據庫的用戶身份運行.exe。 –
如果您在同一臺計算機的Sql Server Management Studio會話中使用_test1 \ test1_,您是否可以連接? – Steve
用戶確實有權訪問,是的,我可以通過SSMS連接到test \ test1 – lemunk