How to connect MySQL Cross Platform?

Hi, im new using xamarin, so i dont have any idea to how make a connection between a remote MySql DB and my project... i will apreciate any help...

BTW sorry for my bad english.

Best Answer

  • EduardoEncinaEduardoEncina US
    Accepted Answer

    Thanks for answering, it was really helpful. I solved my problem doing the next.-

    1.- I had my MySql db on a localhost server so i made a php file using Mysqli Api.

    <?php
    
    $host_db = "localhost";
    $user_db = "root";
    $pass_db = "root";
    $db_name = "ece_admin";
    $tbl_name = "appusuarios";
    
    $conexion = new mysqli($host_db, $user_db, $pass_db, $db_name);
    
    if ($conexion->connect_error) {
     die("La conexion falló: " . $conexion->connect_error);
    }
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $sql = "SELECT * FROM $tbl_name WHERE usuario = '$username'";
    $result = $conexion->query($sql);
    
    if ($result->num_rows > 0) {     
    
         $row = $result->fetch_array(MYSQLI_ASSOC);
         if ($password == $row["contra"]) { 
            echo "Success";
         } else { 
           echo "upX";
         }
     }
     else
     {
        echo "upX";
     }
     mysqli_close($conexion);
     ?>
    

    2.- Then install the nuget packages "Microsoft.Net.http, Microsoft.BCL and Newtonsoft.Json" in the project solution.
    3.- After that i add the next code to my class where i needed to make the connection.

    var user = User.Text;
        var pass = Pass.Text;
    try
            {
    
                var postData = new List<KeyValuePair<string, string>>();
                postData.Add(new KeyValuePair<string, string>("username", user));
                postData.Add(new KeyValuePair<string, string>("password", pass));
    
                var content = new FormUrlEncodedContent(postData);
    
                HttpClient client = new HttpClient();
    
                client.BaseAddress = new Uri("Http://*.*.*.*:81");
    
                var response = await client.PostAsync("Http://*.*.*.*:81/Cinfo.php", content);
                result = response.Content.ReadAsStringAsync().Result;
    
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.ToString(), "Ok");
                return;
            }
    

    And that's how i made it, it works for me, maybe someone can use this or can recomend other way to do it.

    And again sorry for my bad english.

Answers

  • TobyKTobyK GBMember ✭✭✭

    It may be worth having a look at WCF, and create a service that exposes your MySQL somehow- either via a windows service or hosted in a website - and then consuming the WCF service from your mobile app. I suggest starting with giving the following a good read... https://developer.xamarin.com/guides/xamarin-forms/web-services/consuming/wcf/

  • EduardoEncinaEduardoEncina USMember
    Accepted Answer

    Thanks for answering, it was really helpful. I solved my problem doing the next.-

    1.- I had my MySql db on a localhost server so i made a php file using Mysqli Api.

    <?php
    
    $host_db = "localhost";
    $user_db = "root";
    $pass_db = "root";
    $db_name = "ece_admin";
    $tbl_name = "appusuarios";
    
    $conexion = new mysqli($host_db, $user_db, $pass_db, $db_name);
    
    if ($conexion->connect_error) {
     die("La conexion falló: " . $conexion->connect_error);
    }
    
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $sql = "SELECT * FROM $tbl_name WHERE usuario = '$username'";
    $result = $conexion->query($sql);
    
    if ($result->num_rows > 0) {     
    
         $row = $result->fetch_array(MYSQLI_ASSOC);
         if ($password == $row["contra"]) { 
            echo "Success";
         } else { 
           echo "upX";
         }
     }
     else
     {
        echo "upX";
     }
     mysqli_close($conexion);
     ?>
    

    2.- Then install the nuget packages "Microsoft.Net.http, Microsoft.BCL and Newtonsoft.Json" in the project solution.
    3.- After that i add the next code to my class where i needed to make the connection.

    var user = User.Text;
        var pass = Pass.Text;
    try
            {
    
                var postData = new List<KeyValuePair<string, string>>();
                postData.Add(new KeyValuePair<string, string>("username", user));
                postData.Add(new KeyValuePair<string, string>("password", pass));
    
                var content = new FormUrlEncodedContent(postData);
    
                HttpClient client = new HttpClient();
    
                client.BaseAddress = new Uri("Http://*.*.*.*:81");
    
                var response = await client.PostAsync("Http://*.*.*.*:81/Cinfo.php", content);
                result = response.Content.ReadAsStringAsync().Result;
    
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.ToString(), "Ok");
                return;
            }
    

    And that's how i made it, it works for me, maybe someone can use this or can recomend other way to do it.

    And again sorry for my bad english.

  • alexgealexge FRMember
    edited February 2017

    Your way is good.

    You can also check at this Xamarin Plugin who allows Xamarin app to connect remote MariaDB/MySQL directly in your app. But in your case, it's not good to proceed with that because your database contains credentials/auth informations. (even if your app is not public or tests purposes)

  • SahaanaVenkatSahaanaVenkat Member ✭✭

    I tried to connect mysql with the php but its shows me as
    connection timeout pls tell me the how to connect mysql with xamarin.mysql.data

Sign In or Register to comment.