Why can not I connect to SQL Server?

0

I try to connect to a local database with the connection string that offers Visual Studio in the object browser SQL .

The code is as follows:

private void Form1_Load(object sender, EventArgs e)
{
    SqlConnection myCon = new SqlConnection(@"Data Source=GS1512;Initial Catalog=sxUnimageDev;Integrated Security=True;User ID=sa;Password=********;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
    myCon.Open();

    MessageBox.Show("Conectado");
}

When running the application, it keeps trying to connect, it does not show error messages.

Finally load the form but the message "Connected" does not execute it.

Thank you if you can help me.

    
asked by Juan Carlos Marmolejo Martínez 04.05.2018 в 22:09
source

1 answer

0

I suggest two things:

  • Do not put the hard link string in your code, because then, if you need to change it for whatever, you will have to recompile your code. It is more effective that you use the configuration file web.config or app.config, and access the values in your code using the System.Configuration.ConfigurationManager class.
  • that you put everything in a try / catch block, put an intercept point in myCon.Open (); and run the debugger. Then the flow should jump inside the catch block, where you can see more details of the error. In fact, the most correct thing is that you use a logger to throw all the errors into a file and be able to analyze it when you need it. I recommend NLog or Serilog.
  • There would be something similar to this:

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            SqlConnection myCon = new SqlConnection(ConfigurationManager.ConnectionStrings["miConexion"]);
            myCon.Open();
    
            MessageBox.Show("Conectado");
        }
        catch (Exception e)
        {
            _mylogger.Error("Oops!!! Algo fue mal: ", ex);
        }
    }
    
        
    answered by 06.05.2018 в 01:48