Problem connecting with MS SQL Server. Same C# code does not work in Xamarin

Asked

Viewed 264 times

0

We are updating the... I was able to access the bank through Android the problem was the port that was not configured. Now there is another question when I do code Debug for android works but if I do debug on the computer local error in SQL. Do I have to give some permission for my desktop app??

The problem with the code has been solved.

I need to connect to SQL by a C# tested app using windows Forms

 try
        {
            SqlConnection conexao = new SqlConnection((string)@"server=SERVIDORHP\PDVNET; database=BDBANCO; uid=sa; pwd=MINHASENHA123;");
             conexao.Open();
            MessageBox.Show("Todo ok");

        }
        catch (Exception ex)
        {
            MessageBox.Show("Erro", ex.Message);
        }

It worked as expected, but when it comes to Xamarim Forms the code.

try
        {



            SqlConnection conexao = new SqlConnection((string)@"server=SERVIDORHP\PDVNET; database=BDBANCO; uid=sa; pwd=MINHASENHA123;");
                            cmd.Connection = conexao;               
            conexao.Open();


            conexao.Close();
            DisplayAlert("Todo ok.","Conexão bem sucedida!", "OK");                
        }
        catch(Exception e)
        {
            DisplayAlert("Erro", e.Message, "OK");                
        }

Dai returns the message

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. (Preview: TCP Provider, error: 25 - Connection string is not Valid)

Note: I downloaded System.Data.Sqlclient(4.6.0) from the Nuget package is the official of Microsoft.

  • Try placing the IP instead of the host name. Ex.: server=192.168.1.1\PDVNET

  • I tried that way too and the same problem

  • I see two possibilities. One is that the device running Xamarin Forms does not see the machine on which Sqlserver is hosted. Another possibility is that the device sees the machine but cannot open the communication port with the Server. Ai has to see if the device is real or emulated, as each has its own approach if that’s the problem.

1 answer

0

Good morning buddy, try it this way.

  string connectionString = "server=SERVIDORHP\PDVNET; database=BDBANCO; uid=sa; pwd=MINHASENHA123;"

         using SqlConnection conexao = new SqlConnection(connectionString)
        {
          conexao.Open();
          DisplayAlert("Todo ok.","Conexão bem sucedida!", "OK"); 
        }

Browser other questions tagged

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