# Visual Basic > Xamarin >  How do Xamarin forms connect to SQL Server ?

## dong

How do I process the xamarin forms that connect to SQL Server and export data to the listView ? I'm looking for this simple example xamarin forms that connect to SQL Server and display data to the listView. What do I find on google ?

----------


## techgnome

I don't know... what did you find on google? Try googling xamarin sql server   .... sometimes just going with the basics works best.

I found this: https://forums.xamarin.com/discussio...-to-sql-server
Which talks about using a REST WebAPI to talk to SQL Server. The xamarin app doesn't do it directly, but invokes the queries through a REST API call over the web.

----------


## NeedSomeAnswers

> I found this: https://forums.xamarin.com/discussio...-to-sql-server
> Which talks about using a REST WebAPI to talk to SQL Server. The xamarin app doesn't do it directly, but invokes the queries through a REST API call over the web.


Yep i second that, your Xamarin app wont talk directly to SQL Server. 

Think about it this way, when you finally publish your mobile app and make it usable on a mobile phone, if it did have a direct link to SQL Server then you would only be able to use that app when you were wirelessly attached to the same network as your SQL Server is on (and therefore your app isn't really very mobile is it!!)

Almost all mobile apps will use a REST web service of some sort to get data,

Your Xamarin forms app will make calls to your web service, your web service talks to the SQL Server and gets your data into a class object or list of object, and then converts it and returns it as JSON to your mobile app. Your mobile app then once it has received that data converts the JSON back into a class object or a list of objects which you can then use in your mobile app. 

Your first step before you do any mobile development should be to create your web service, write you code to pull data form SQL server and get you web service to return data. 

Once you have at least one web service call working and returning data successfully you can then look to hook it up to your mobile app.

----------


## dong

I'm looking for this simple example on google no, do you know site that supports demo library using REST WebAPI with SQL Server? The page you submitted has no example https://forums.xamarin.com/discussio...-to-sql-server

----------


## Prahlad

1.Add in a System.Data reference to your project
2.Add in a using directive "using System.Data.SqlClient"
3.Define connection string and open the connection using SqlConnection(connectionString)
4.Manipulate database as normal


string connectionString = @"Server=<ipAddress>;Database=<DBName>;User Id=<username>;Password=<password>;Trusted_Connection=true";
string databaseTable = "<yourDBTableName>";
string referenceAccountNumber = "0001134919";
string selectQuery = String.Format("SELECT * FROM {0} WHERE [Account_Number] = '{1}' ", databaseTable, referenceAccountNumber);




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;
        }
    }
    catch (Exception exception)
    {
        #region connection error
        AlertDialog.Builder connectionException = new AlertDialog.Builder(this);
        connectionException.SetTitle("Connection Error");
        connectionException.SetMessage(exception.ToString());
        connectionException.SetNegativeButton("Return", delegate { });
        connectionException.Create();
        connectionException.Show();
        #endregion
    }

----------


## NeedSomeAnswers

> 1.Add in a System.Data reference to your project
> 2.Add in a using directive "using System.Data.SqlClient"
> 3.Define connection string and open the connection using SqlConnection(connectionString)
> 4.Manipulate database as normal


No No No 

Do not even think about doing this approach, your on a mobile device not a desktop app. It will not work

----------


## NeedSomeAnswers

> I'm looking for this simple example on google no, do you know site that supports demo library using REST WebAPI with SQL Server?


There are plenty of example of building a REST WebApi talking to SQL server

if you want to do it in normal .net 

https://docs.microsoft.com/en-us/asp...aspnet-web-api

if you want to use .Net Core

https://docs.microsoft.com/en-us/asp...=visual-studio

However if your asking for a simple example to for using a Web API with Xamarin then you are out of luck, its not simple. Its perfectly learnable but you need to know MVVM and if you dont know it you spend sometime understanding it.

There are no short cuts, if you look on YouTube there are now a lot of good tutorials on Xamarin and a lot of using Xamarin with MVVM and Web API if you search for those terms.

----------

