0
votes

I am attempting to run a program that gets data from a remote sql server, it works for a coworker from another remote site but when I run the program the debugger says that Sql_Reader is returning null.

I can ping and connect to the remote sql server. The instance is correct as well as the firewall settings on the server per http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx However when I run a program to get data from the sql server there is and error that says that remote server was not found or not accessible.

I checked the connection string per error: 26 - Error Locating Server/Instance Specified. (Can't connect to my local Db From my host server)

This is the connection string that is in the GetConnectionString method.

    return @"Data Source= 2.2.2.2\SQLEXPRESS; Persist Security Info=False;User ID=myusername;Password=mypassword;Initial Catalog=SomeDataStructure;";

The following error happens on the line that has sql_Connection.Open();

SqlException was Unhandled. 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)

    // Body of the method
    str_Query = "SELECT " + dict_SearchData["FieldName"] + " FROM " + dict_SearchData["Table"] +
            " WHERE " + dict_SearchData["IndexColumn"] + " = '" + dict_SearchData["IndexRow"] + "';";
        using (SqlConnection sql_Connection = new SqlConnection(dict_SearchData["ConnectionString"]))
        {
            SqlCommand sql_Command = new SqlCommand(str_Query, sql_Connection);
            sql_Connection.Open();
            SqlDataReader sql_Reader = sql_Command.ExecuteReader();
            try
            {
                sql_Reader.Read();
                str_FieldValue = sql_Reader[0].ToString();
            }
            catch (System.InvalidOperationException)
            {
                //ignore the exception 
            }
            finally
            {
                // Always call Close when done reading. 
                sql_Reader.Close();
            }
        }
2
A common error message with many possible causes. blog.sqlauthority.com/2009/05/21/…mr.Reband
Is SQL Server allowing remote connections? Also is the instance configured to use SQL Server auth?Milen

2 Answers

3
votes

I added the port number to the connection string and I was able to connect to the database remotely with the application. I added the port number after the ip address following the comma.

return @"Data Source= 2.2.2.2,1433\SQLEXPRESS; Persist Security Info=False;User ID=myusername;Password=mypassword;Initial Catalog=SomeDataStructure;";
1
votes

It looks like your connection string may have extra characters or be formatted incorrectly. Try taking the extra step of putting the string in a local string variable, then assigning it to the ConnectionString object. This way you can step through the code and inspect the string value discretely. Also, the more common custom is to specify the server name as opposed to the ip address, although I would think either should work. Also, that backslash may be creating problems, might have to escape that character.