Search code examples
c#sql-serverxamarin.formssqlconnection

Using SqlConnection in Xamarin.Forms?


Can I use SqlConnection in Xamarin.Forms?

I tend to use this in Windows and Web applications developed in Visual Studio, but I don't know how it would work in Xamarin.

I get namespace name does not exist with using System.Data.SQLClient;, even after adding references Mono.Data.Sqlite and System.Data.

        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = SQL;
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();

        conn.Open();

        da.Fill(ds);
        conn.Close();

Can this code snippet be used or does it need to be done another way?

I'm adding the code to a class that's part of the Xamarin.Forms project.


Solution

  • There is no way to make SQL connection. In general platforms supported by Xamarin have no support for traditional databases (except for SQLite if you can count it in there), so such connection wouldn't make sense.

    If you want to connect to real databases in Xamarin, those databases must have the web interface. And again that is not Xamarin limitation but rather limitation of nearly all supported platforms.