How to connect to a local SQL Server db from Xamarin VS android app

0

I have a working SQL Server database on my laptop, a smart phone linked to the laptop by USB and wi-fi. I have the following code to connect to the database:

                string connectionString = @"Data Source=192.168.1.102,1433;Initial Catalog=MyInsurance_database;Integrated Security=SSPI;Connection Timeout=15";
            string databaseTable = "SurveyVariants";
            string whereAnd = "5";
            string selectQuery = String.Format("SELECT * FROM {0} WHERE RecNo = {1}", databaseTable, whereAnd);

            try
            {
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    //open connection
                    connection.Open();

                    SqlCommand command = new SqlCommand(selectQuery, connection);

                    command.Connection = connection;
                    command.CommandText = selectQuery;
                    var result = command.ExecuteReader();
                    //check if account exists
                    var exists = result.HasRows;
                    Toast.MakeText(this, "Success - " + exists, ToastLength.Short).Show();
                }
            }
            catch (Exception exception)
            {

I know that the SQL string is correct and that in SSMS it returns one record correctly. I have tried various permutations of Server= instead of Data Source=, Initial Catalog= instead of Database=, with and without the port number as 1433, 1434 or 80, after making a new rule for the firewall. Error messages begin:

System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 25 - SNI_ERROR_25) Snix_Connect (provider: SNI_PN7, error: 25 - SNI_ERROR_25) ---> System.Net.Sockets.SocketException (0x80004005): The socket is not connected at System.Data.SqlClient.SNI.SSRP.GetPortByInstanceName etc. or System.Data.SqlClient.SqlException (0x80131904): Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40) Snix_Connect (provider: SNI_PN7, error: 40 - SNI_ERROR_40) etc.

What else can I try - any ideas? Thank you.

android
sql-server
xamarin
asked on Stack Overflow Sep 11, 2018 by Jonathan T

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0