2

I'm just a beginner in using Xamarin. I created a sample Xamarin.Forms Portable project in Visual Studio 2013. I want to know if it is possible to access an MS SQL database and display it to my mobile phone? If Yes, can you please give me some instruction on how am I going to do this? Thanks a lot. I hope someone will help me.

5
  • 2
    Yes you can
    – Martheen
    Commented Jun 17, 2016 at 4:18
  • @Martheen thanks for the answer. Can you please give me instructions on how will I do this? Commented Jun 17, 2016 at 4:39
  • 2
    That's not what stackoverflow for. The link I've given contain enough instruction. Try them first, write the codes, then ask if you found a problem.
    – Martheen
    Commented Jun 17, 2016 at 4:41
  • @Martheen where's the link you're talking about? Commented Jun 17, 2016 at 4:50
  • The 'can' word is clickable
    – Martheen
    Commented Jun 17, 2016 at 5:29

1 Answer 1

7

You cannot access directly an sql server from your pcl project in Xamarin.Forms because System.Data.SqlClient is not available on pcl.

But you can do it through a dependency service.

First in you PCL project declare you service

public interface IDbDataFetcher
    {
        string GetData(string conn);
    }

Then on you Android project implement the service interface

[assembly: Dependency(typeof(DbFetcher))]
namespace App.Droid.Services
{
    class DbFetcher : IDbDataFetcher
    {

        public List<string> GetData(string conn)
        {
            using (SqlConnection connection = new SqlConnection(conn))
            {

                SqlCommand command = new SqlCommand("select * from smuser", connection);
                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        data.Add(reader[0].ToString());
                    }
                    reader.Close();
                }
                catch (Exception ex)
                {
                    //Console.WriteLine(ex.Message);
                }
            }
            return data;
        }
    }
}

Although it is a solution it is a bad one. Always consume web services for your mobile apps

3
  • Thanks for the answer Sir. I have created a sample project and tried using REST web service API that I've watched in a tutorial. And have it checked with some other resource. They said that I do not have error in code but maybe in binding. Since I'm just a newbie in using Xamarin, I wasn't able to find out what's the cause of my problem. Can you check it anyway? Thanks a lot. Commented Jun 18, 2016 at 2:26
  • 1
    @Jaycee, Could you give your tutorial's address? Commented Jun 27, 2016 at 9:01
  • @JayceeEvangelista, Is this code established connection and fetch data? Commented Mar 5, 2018 at 7:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.