-3

I'm using Xamarin to connect to SQL Server while making a mobile application. The application runs on iOS. I am using sql client to establish a connection with the server, but I'm running into weird errors and have exhausted most of my options.

Please do not warn me about security issues of direct connections. This is not the concern.

The error I'm receiving is this: provider: tcp provider, error: 40 - could not open a connection to sql server)

Here is my code:

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

namespace EmployeeSnapshot
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        { 
            InitializeComponent();
        }

        void Button_Clicked(System.Object sender, System.EventArgs e)
        {
            var EmployeeID = UserInput.Text;
            Console.WriteLine(EmployeeID);

            string con = "Data Source ={Server}; Initial Catalog ={database}; Integrated Security = False; User ID ={username}; Password ={password}";
            string matchingPerson;

            using (SqlConnection connection = new SqlConnection(con))
            {
                string oString = "SELECT something FROM table WHERE employeeid = @employeeID";

                SqlCommand command = new SqlCommand(oString, connection);
                command.Parameters.AddWithValue("@employeeID", EmployeeID);

                connection.Open();

                Console.WriteLine("State: {0}", connection.State);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    matchingPerson = reader["name"].ToString();
                }

                command.Connection.Close();
            }

            Console.WriteLine(matchingPerson);
        }
       
    }
}

The code errors out at the Connection.open() line.

What I've tried:

  • changed the connection string around, messed with integrated security, ensured the servers are up and running, etc.
  • I am making this app on a MacBook. I have parallels, so I can use a virtual machine to go into windows. I decided to take advantage of this, and copied my connection string and pasted it into visual studio on the windows side. I performed a basic select statement and retrieved the data no problem, so the connection string doesn't seem to be the issue.
  • I almost feel like it has something to do with Xamarin or with the fact I'm on a MacBook.
izzykk
  • 31
  • 6
  • If you are calling this from xamarin, the chance is you are using an external device or emulator. This means you are doing a remote connection. This is not a pattern you want for security. As theconnectionstring will be included in the code to access the database. Maybe you need an API to access your database. – Preben Huybrechts Jul 01 '20 at 11:31
  • I understand that Preben - as stated in the question I am not concerned with this. I know you can establish a direct connection in Xamarin and I'm trying to accomplish that. – izzykk Jul 01 '20 at 11:32
  • @izzykk Could it be blocked by a firewall? – Andrew Morton Jul 01 '20 at 11:34
  • @AndrewMorton I suppose that's possible. But I have parallels and am able to connect to SQL Server just fine on the windows side - the Mac side is where things go wrong. I'll try looking into that again though – izzykk Jul 01 '20 at 11:39
  • The Server in connection string should include both a server and instance like .\SQLExpress.Windows may be blocking your connection to a remote machine. When a connection is made between two machines encryption is used and the two machines need to be in same Group so communications is allowed.I recommend using Integrate Security = true and remove the Username and password.Then the user account need to be the same on local machine and remote machine.Test if the username/password is an issue use SQL Server Management Studio from machine where code is failing and use same credentials in login. – jdweng Jul 01 '20 at 11:51
  • I tried adding the instance name but got "error: unrecognized escape sequence" – izzykk Jul 01 '20 at 12:19
  • @izzykk Did you forget to escape the backslashes? – Andrew Morton Jul 01 '20 at 12:41

1 Answers1

0

I was right - the problem was with Xamarin/The visual studio project itself. All I did was clean and rebuild the solution to get everything to work. Xamarin is buggy and when in doubt, from this experience, I have learned to clean and rebuild when libraries aren't acting as expected.

E_net4
  • 27,810
  • 13
  • 101
  • 139
izzykk
  • 31
  • 6