我試圖用c#遠程連接到SQL Server 2005 Express版本,但它引發異常。無法使用c#遠程連接SQL Server 2005 Express
A network related or instance specific error occured while establishing a connection to SQL Server .The server was not found or was not accessible.Verify that the instance name is correct and the SQL Server is configured to allow remote connection. (provider :Name Pipes Provider,error:40 - Could not open a connection to SQL Server)
下面是我使用連接到數據庫
private void button_test_Click(object sender, EventArgs e)
{
try
{
string str = "data source=" + textBox_server.Text + "; initial catalog=" + textBox_db.Text
+ "; user id=" + textBox_user.Text + "; pwd=" + textBox_password.Text + ";";
SqlConnection sqlcon = new SqlConnection(str);
sqlcon.Open();
sqlcon.Close();
MessageBox.Show("Test Connection was successfull");
}
catch (Exception ex)
{
MessageBox.Show("Test Connection failed. "+ ex.Message);
}
我輸入正確的IP,數據庫表名,用戶名和密碼的代碼。
驗證了SQL Server Express配置中的所有內容。一切安好。
那麼我哪裏錯了?
sql server是獨立PC上的嗎?如果是這樣,你可能需要打開一個特定的端口。 – Derek
你沒有提到防火牆,你將需要配置Windows防火牆,以允許連接(在SQL Express運行的PC上) –
你已經爲兩臺PC配置了Windows防火牆 – Sohail