2012-09-18 110 views
0

我有一個asp.net網站,我的本地計算機上工作得很好,它有一個函數來創建和打開連接(SQL服務器2008 R2) 我買了VPS和我上傳了我的源它,當我把它無法連接到數據庫的功能,因爲數據源是錯誤的! 它的錯誤就收到:數據源無效

{System.Data.SqlClient.SqlException: 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) 

這裏是我的代碼,我的電腦上工作:

public ClientHandle() 
    { 
     SqlConnection con = new SqlConnection("Server=localhost;Data Source=ARASH-PC;User ID=sa;password=*****;Initial Catalog=Ranker;Trusted_Connection=False;Integrated Security=False;"); 
    con.open(); 

    } 

當我上傳了我的源到我的VPS,我改變了數據源到我的IP:

public ClientHandle() 
    { 
     con = new SqlConnection("Server=localhost;Data Source=209.54.48.101;User ID=sa;password=*****;Initial Catalog=Ranker;Trusted_Connection=False;Integrated Security=False;"); 
    } 

但它無法連接到數據庫!什麼是錯我的代碼?什麼是我的錯?

回答

1

試試這個:

con = new SqlConnection("Server=209.54.48.101;Database=Ranker;User ID=sa;Password=*****;Trusted_Connection=False;Integrated Security=False;"); 

爲SQL Server連接字符串2008

標準安全

Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername; Password=myPassword; 

OR

Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False; 
+0

坦,它解決了我的問題:) –