How do i connect from Xamarin.Forms Cross platform to SqlServer

blackcatblackcat Member

How do i connect from Xamarin.Forms Cross Platform to SQL Server, It just hangs and does not even show the DisplayAlert()

My code looks something like this

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

namespace SqlConExample
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

    private async void connectBtn_Clicked(object sender, EventArgs e)
    {
        string constring = @"Data Source=.\SQLExpress;Initial Catalog=people_Database;Integrated Security=True";
        using (SqlConnection con = new SqlConnection(constring)) 
        {
            try
            {
                con.Open();
                await DisplayAlert("OK", "OK i am Connected", "OK");
            }
            catch (Exception ex)
            {
               await DisplayAlert("Error", ex.ToString(), "OK");
            }
        }
    }
}

}

[/code]

Why does it hang?

Answers

  • JohnHardmanJohnHardman GBUniversity mod
    edited November 22

    @blackcat said:
    How do i connect from Xamarin.Forms Cross Platform to SQL Server

    There are many existing posts and articles about this, even books covering the subject too.
    The usual solution is to have your Xamarin.Forms app communicate with a Web API using REST. The Web API implementation communicates with the SQL Server database, so that you Xamarin.Forms code does not interact directly with the database.

    If you use the forum's search functionality, or use your favorite search engine, you should have no problems finding details of REST, Web APIs and their use with Xamarin.Forms.

Sign In or Register to comment.