Results 1 to 7 of 7

Thread: How do Xamarin forms connect to SQL Server ?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    475

    How do Xamarin forms connect to SQL Server ?

    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 ?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,394

    Re: How do Xamarin forms connect to SQL Server ?

    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,649

    Re: How do Xamarin forms connect to SQL Server ?

    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.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    475

    Re: How do Xamarin forms connect to SQL Server ?

    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

  5. #5
    Lively Member
    Join Date
    Jan 2020
    Posts
    121

    Re: How do Xamarin forms connect to SQL Server ?

    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
    }

  6. #6
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,649

    Re: How do Xamarin forms connect to SQL Server ?

    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
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  7. #7
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,649

    Re: How do Xamarin forms connect to SQL Server ?

    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.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width