not able to use PCL neither Shared library for SQLconnection code

Hello,
I am new to Xamarin and I am trying to build an app, which would use SQLite server. connection. I've already written all my SQLite code and tested in Console Application in C# (Desktop app). Now I want to migrate it into Xamarin native application.

I have 3 projects - iOS, Android and PCL. The thing is, that the code can be compiled if I add a class into both projects iOS and Android (after adding system.data reference). That's actually not correct approach, because I am duplicating the code.

I would like to use some kind of shared place, which would be used by iOS and Android project - I guess PCL/Shared Library project should be used for this purposes.
Problem is, that I cannot add system.data reference into PCL neither to Shared Project library.

Following abstract class I am using:

using System;
using System.Collections.Generic;

namespace DBConnection.Model
{
    public abstract class Database
    {
        protected string ConnectionString { get; set; }
        protected abstract DbConnection CreateConnection();
        protected abstract DbCommand CreateCommand(string commandText, DbConnection connection);

        public abstract DataTable ExecuteSelect(string query, Dictionary<string, object> parameters = null);
        public abstract int ExecuteCommand(string query, Dictionary<string, object> parameters = null);
    }

    public enum DataType
    {
        SqlLiteDatabase,
        SqlDatabase
    }
}

Thanks a lot for explanation,
Tomas

Best Answer

Answers

  • mikeacostamikeacosta USUniversity ✭✭✭

    System.Data, and ultimately System.Data.SqlClient is not built as a PCL and can't be referenced in your PCL (compared to something like SQLite.NET for local DB access, which can be). You would need to do something like create an interface in your PCL and reference System.Data.SqlClient in the implementation of that in each platform-specific project. Then use the DependencyService or some other dependency resolver to use that in your PCL.

    But instead, you really s/b accessing remote data in a Forms project using a RESTful API:

    https://xamarinhelp.com/connecting-remote-database-xamarin-forms/

  • IzmailIzmail CZMember ✭✭
    edited April 2017

    Thanks @mikeacosta for answer. Unfortunately it's still not fully clear to me. Most probably I am missing some basics.

    I still have these questions, which are in my head:
    1. If System.Data cannot be referenced, why particular projects like iOS and Android can reference them?
    2. How does interface solve my issue in terms of code duplication? The real implementation would be the same for both iOS and Android, but I would have to implement it for both separately. Maybe I confused you a bit, because I attached abstract class. The idea was, that derived class public class SqlLiteDatabase : Database will be in PCL as well.
    3. About DependencyService - first time I hear about that. I will find out more information in the internet.
    4. Thanks for REST API link. I will use is for sure in the future implementation (at this moment I want to access just SQLite).

    Thanks for help,
    Tomas

  • IzmailIzmail CZMember ✭✭
    edited April 2017

    Thanks for answer, I have couple of comments as it is still not fully clear to me.

    @mikeacosta said:
    System.Data, and ultimately System.Data.SqlClient is not built as a PCL and can't be referenced in your PCL (compared to something like SQLite.NET for local DB access, which can be). You would need to do something like create an interface in your PCL and reference System.Data.SqlClient in the implementation of that in each platform-specific project.

    Maybe I do not understand correctly, but it means, that instead of abstract class I will use interface, but concrete implementation will have to be at iOS or Android level. Which means I am going to duplicate code.

    Then use the DependencyService or some other dependency resolver to use that in your PCL.

    I do not use Xamarin.Forms. Can dependency service be used for native?

    But instead, you really s/b accessing remote data in a Forms project using a RESTful API:
    https://xamarinhelp.com/connecting-remote-database-xamarin-forms/

    At this moment my application will focus primarily on SQLite connection. Then I will extend it to MS SQL (remote connection) and I will use REST - thanks for tip.

    Maybe problem is, that I do not understand basics, but what is point of having PCL (where I would put shared code) if I cannot use System.Data. I would understand that Xamarin does not support it. But why iOS project as well Android project supports it?

    Thanks

Sign In or Register to comment.