简体   繁体   中英

SQL Server error when trying to connect

So I am having a problem connecting to my sql server.

I know 100% its working and accepting connections, but for some reason I keep getting an error.

I can browse the server just fine in Server Browser, and If I bind the datasource to a datagrid, I can preview the table as well. So I know it's working fine inside VS, but not runtime.

From my knowledge and experience, if VS can connect to the server and display all the good stuff, then it should be able to do it during runtime??

Any help and advice is much appreciated.

Non Duplicate Reason The reason I believe my question is not a duplicate, is because my application is not a ASP or an MVC app. It's a winforms application. So many of the suggestions do not work for me. I have written 2 c# programs now (one wpf and one winform) to access our SAP database and they work just fine. The SAP database is located on the same server as the one I am trying to connect to. I can browse the server perfectly fine in SQL-Server Management Studio on my machine. I can also browse the server perfectly fine in Visual Studios server browser. But When I debug the program, it halts with this error.

My Connection code

public string connectionString = @"Data Source=ENGSRV\SIGMANEST;Initial Catalog=SNDBase10;Persist Security Info=True;User ID=XXXXX;Password=XXXXX";
private void btnSearch_Click(object sender, EventArgs e)
{
    SqlConnection connection = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand();

    try
    {
        try
        {
              connection.Open();
              MessageBox.Show("Connected");
        }
        catch (Exception ex)
        {
                    throw;
                    //MessageBox.Show(ex.Message, "Database Error!");
        }
   }
   catch (Exception ex)
   {
         throw;
         //MessageBox.Show(ex.Message, "Connection Error!");
    }
}

Error Code

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in ProductionOrderQuery.exe

Additional information: 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)

Server Explorer Screen Shot

Table Data Rows Screen Shot

Update: So after thinking about it tonight, I believe it has to do with my firewall and nothing to do with the sql server or the firewall on the server. Here's why, My VB.Net application that can successfully connect to the sql server is not a standalone application. It's a plugin for a CAD software called Rhinocreos 5. All our CAD Designers have 0 problems pulling data from the sql server from within Rhino... Visual Studio can connect with 0 problems in the Server Explorer... Which now leaves me to believe that the application(plugin), is piggy-backing of a firewall rule Rhinocreos 5 made during its installation. If I change the connection string to the one I posted here, it connects perfectly. But it will not find the sql server instance in my standalone application.

That being said, I guess I need to research on how to promaticaly add my program to the clients firewall rules to access the network.

Thanks to everyone that has helped my solve this. If anyone has any tips or pointers to allowing my program through firewalls on any client it's installed on, let me know.

Problem was within our network.

Our IT guy decided to have all our computers "My Documents" on a separate server and NOT on our own machines.

So Visual Studio by default saved my project to this server and not to my own computer. This was causing the block to the SQL Server.

I solved this by simply moving my project to my actual documents folder on my computer.

Thanks everyone for the help and pointers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM