sqlconnection中的数据源无效

时间:2012-09-18 07:30:32

标签: asp.net sql-server-2008 vps

我有一个在我的本地计算机上工作正常的asp.net网站,它有一个创建和打开连接的功能(sql server 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 个答案:

答案 0 :(得分: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;