-3

I have a Xamarin.Android application and I want the android emulator to get access to my database on localhost, I want to connect my application to my database by IP address. Here is the code I have so far:

var gridview1 = FindViewById<GridView>(Resource.Id.gvd);
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1);

SqlConnection con = new SqlConnection("Data Source = localhost:1433;Initial Catalog = DB_B2B; Integrated Security = True");
SqlCommand cmd = new SqlCommand("select * from en_cours");
con.Open();
cmd.Connection = con;
SqlDataReader sqlreader = cmd.ExecuteReader();
while (sqlreader.Read())
{
    adapter.Add((string)sqlreader[0]);
}
sqlreader.Close();
con.Close();

This is the error I receive when trying to connect:

System.Data.SqlClient.SqlException:

Snix_Connect (provider: SNI_PN9, error: 44 - SNI_ERROR_44)

Snix_Connect (provider: SNI_PN9, error: 44 - SNI_ERROR_44)

Iain Smith
  • 9,230
  • 4
  • 50
  • 61

1 Answers1

1

On Android emulators, if you want to connect to the hosting machine you should use 10.0.2.2 and not localhost.

If you use 127.0.0.1 or localhost, the App will try to access the local network on the emulator itself, which is not the same network as the host machine.

This is also answered in this StackOverflow QA: How do you connect localhost in the Android emulator?

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118