0

I have an issue trying to manually connect to a sql server on my network.

I have a web API with a conn string that works fine, I have a VS2015 winforms project i set up to test the conn using the VS tool to connect, this also works fine.

However when i try to connect via SQLconnection it fails and says the server cannot be found or isnt accessable.

So heres the test code...

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();
        }

Now i dont really care about results or anything i simply need to establish a connection. I cant figure out why it throws this error, when my Web API works and the VS tool for connection to DBs works fine too.

I have checked the server and enabled named pipes too just to make sure.

Any ideas?

EDIT:The error i catch...

 {"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)"}

happens after about 15 seconds trying to open the connection. The thing is this connectiin string i tried is the exact same string used via the SQL tool, I did this so i knew i had a valid string.

ODBC connection also falls over with the same message.

I checked from my machinbe is i can listen to port 1433 but it says the server isnt listening. However looking at the server its set to dynamic ports with a range of any to 41934, so im at a loss as to why it says its not listening evenb though its to dynamic ports

EDIT2: Well, must be permissions or drivers... just tried the exact same code and worked instantly...

This is diving into the realm of the unknown i feel. might ask for deletion of the question if no solution is found

6
  • 1
    Maybe the user under which the WinForm app is running doesn't have the access to DB? Try running the .exe as a user that can access the database. Commented Sep 28, 2016 at 7:31
  • If you use that test1\test1 in a Sql Server Management Studio session from the same machine are you able to connect? Commented Sep 28, 2016 at 7:32
  • User does have access, and yes i can connect to test\test1 via SSMS Commented Sep 28, 2016 at 7:33
  • What error do you get from your code? Commented Sep 28, 2016 at 7:33
  • Edited to include the full error Commented Sep 28, 2016 at 7:36

1 Answer 1

1

I think that i see the problem:

string connectionString = @"Data Source=test1\test1;Initial Catalog=my_test;Integrated Security=True;";

Integrated Security is used for local logins, You could make an account in the database and change the connection string to:

string connectionString = @"Data Source=test1\test1;Initial Catalog=my_test;User id=<Username>;Password=<Password>;";

I hope this helps!

Sign up to request clarification or add additional context in comments.

7 Comments

You could use the ip of the sql server in your network.
thats the only thing i havent tried yet, im currently restricted messing with it (new guy) but thats the only avenue i havent looked at yet i dont think
Ok so port 1433 isnt listening, however the server is set up using dynamic, so would this make a difference at all?
I am not sure about that, You could try to add port 1433 in your inbound rule and outbound rule in your firewall and see if it works.
firewall is turned off for domain, but private and public networks its on fully blocking all apps. Sql conn connects via tcp, surely it wouldnt be running through the othewr firewall as were on the network??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.