Home > other >  Make the connection of my app in Xamarin.Forms to a SQL Server database through a web service?
Make the connection of my app in Xamarin.Forms to a SQL Server database through a web service?

Time:08-09

I need to connect my Android/iOS mobile app with a database in SQL Server. I was able to make the connection directly but I have read that the correct way is to do it through a web service but I am not very clear about how it works or how I should implement it. I appreciate that someone can help me or provide me with a link or tutorial where I can see how to do it

This is the master connection class in which you made the direct connection and later I call the methods in the pages where I need them, but it is in the direct way

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;


namespace MVVMdemo.Modelo
{
    class MCONEXIONMAESTRA
    {
        public static string srvrdbname = "***";
        public static string srvrname = "***";
        public static string srvrusername = "***";
        public static string srvrpassword = "***";
        public static string conexion = $"Data Source={srvrname};Initial Catalog={srvrdbname};User ID={srvrusername};Password={srvrpassword}";
        public static SqlConnection conectar = new SqlConnection(conexion);

        public static void Abrir()
        {
            if(conectar.State == ConnectionState.Closed)
            {
                conectar.Open();
            }
        }

        public static void Cerrar()
        {
            if (conectar.State == ConnectionState.Open)
            {
                conectar.Close();
            }
        }
    }
}

CodePudding user response:

You can make a DBConnect class like this:

//Add MySql Library
using MySql.Data.MySqlClient;
 class DBConnect
    {
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
    
        //Constructor
        public DBConnect()
        {
            Initialize();
        }
    
        //Initialize values
        private void Initialize()
        {
            server = "localhost";
            database = "connectcsharptomysql";
            uid = "username";
            password = "password";
            string connectionString;
            connectionString = "SERVER="   server   ";"   "DATABASE="   
            database   ";"   "UID="   uid   ";"   "PASSWORD="   password   ";";
    
            connection = new MySqlConnection(connectionString);
        }
    
        //open connection to database
        private bool OpenConnection()
        {
           connection.Open();
           return true;
        }
    
        //Close connection
        private bool CloseConnection()
        {
          connection.Close();
          return true;
        }
   
    }

CodePudding user response:

In order to connect through a web service you will need to host the SQL Server and database somewhere. There are some SQL providers that make this easy and are free but they will all require some setup. I've used SQLLite, MongoDb, and others before. You could also host it on Azure or AWS but that will likely result in some charges as well.

Once you get the server and db up and running, you can then place the connection strings in place of your local one. However, since you are using Xamarin and C#, I would highly recommend looking into the EntityFramework and the SQL libraries Microsoft provides as they will make it even easy for you to connect to databases and work with your data while still remaining secure.

  • Related