Error in string when connecting sql server database remotely

Asked

Viewed 301 times

0

I’m a beginner in C# and I’m trying to remotely connect my application with an SQL Server database that is on another PC... In this PC has already been configured the TCP/IP port following this link model: http://www.sqlfromhell.com/habilitando-a-conexao-remota-no-sql-server/

If I try to connect via SQL Management Studio with the PC IP, IT WORKS!

But in my C# application always returns the error: "System.Data.Sqlclient.Sqlexception: 'Instance-specific or network error when connecting to SQL Server. The server was not found or not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (Preview: SQL Network Interfaces, error: 26 - Server Locate Error/Specified Instance)'"

It follows how I have structured my connection:

App.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="Sistema.Properties.Settings.SistemaConnectionString" connectionString="Data Source=xxx.xxx.x.xxx,1433;Network Library=DBMSSOCN;Initial Catalog=meusistema;User ID=sa;Password=********" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="HtmlAgilityPack" publicKeyToken="*************" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.11.12.0" newVersion="1.11.12.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Connection Class:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Sistema
{
    class Conexao
    {
        public Conexao()
        {
        }
        public static SqlConnection Conectar()
        {
            SqlConnection conn = new SqlConnection("SERVER=xxx.xxx.x.xxx\\SQLEXPRESS;DATABASE=Sistema;Integrated Security=SSPI");
            conn.Open();

                return conn;
        }
    }
}
  • Why are you using two different connection strings? In the app.config you have one and in the connection class you have another

  • In the app.config mount the connection string with password and everything else... in the connection class use to instantiate the connection... works (when I had the sql server database installed on the same PC where I am developing the program). But if you have any better idea of how I can structure this connection you’re welcome!

  • Usually use the app.config connection string there! The app.config should centralize your project settings.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.