-1

Greetings to the whole community. I am developing a mobile application in the Unity 3D environment. To make the connection with the SQL Server database hosted in Azure, I am using the "using System.Data.SqlClient" library. The connection works correctly when running the application inside the Unity environment. The problem is that when generating the apk of the application and running it on a mobile device, the application cannot connect to the database in the cloud. Is there any way to fix this problem without having to resort to using php or web api, otherwise you would have to rewrite all the application code. Thank you very much for your help.

Connection to AzureDataBase

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Data;
using System.Data.SqlClient;
using System;

public class Conexion : MonoBehaviour
{
    public static TextMeshProUGUI lblConexionValida;
    public static TextMeshProUGUI lblConexionInvalida;
    public static string cad = "connection string";
    
    public static String ChequearConexion()
    {
        String mensaje = "";
        SqlConnection SqlConexion = new SqlConnection();
        try
        {
            SqlConexion.ConnectionString = Conexion.cad;
            SqlConexion.Open();
            mensaje = "Y";
        }
        catch (Exception ex)
        {
            mensaje = ex.Message;
        }
        finally
        {
            SqlConexion.Close();
        }

        return mensaje;
    }

    public SqlConnection conectarme()
    {
        SqlConnection cn = new SqlConnection(cad);
        return cn;
    }
}

Example of how I am querying the database:

public int recuperarIdODN()
{
    int idOdn = 0;
    Debug.Log("nOMBRE DE ODN :" + GestorOdn.NombreOdn);
    SqlConnection con = new SqlConnection(Conexion.cad);

    string qry = "select o.IdOdn from Empresa as e Join Odn as o on e.IdEmpresa = o.IdEmpresa where e.IdEmpresa = " + LoginPanelManager.idEmpresa + " and o.NombreOdn  like '%" + nombreODN.captionText.text + "%' ;";

    Debug.Log("Query: " + qry);

    var cmd = new SqlCommand(qry, con);
    cmd.CommandType = CommandType.Text;
    con.Open();
    using (SqlDataReader objReader = cmd.ExecuteReader())
    {
        if (objReader.HasRows)
        {
            while (objReader.Read())
            {
                idOdn = objReader.GetInt32(objReader.GetOrdinal("IdOdn"));
            }

            con.Close();
            Debug.Log("Id del ODN :" + idOdn);
            return idOdn;
        }
        else
        {
            Debug.Log("Error, ODN no registrado");
            con.Close();
            return 0;
        }
        // reporODN.captionText.text = NombreOdn;
    }
}
burnsi
  • 2,169
  • 1
  • 10
  • 20
Jefferson
  • 1
  • 1
  • https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question#:~:text=Images%20are%20harder%20to%20read,actual%20code%20and%20formatting%20it. – Sean Lange Jun 01 '22 at 19:27
  • I would also suggest using parameters in your code. Why? https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements – Sean Lange Jun 01 '22 at 19:29
  • Welcome to StackOverflow. [Please don't use images of code in questions.](https://meta.stackoverflow.com/q/285551/295004). Please read [JDBC vs Web Service for Android](https://stackoverflow.com/q/15853367/295004) as the answer applies. – Morrison Chang Jun 01 '22 at 19:29

0 Answers0