Jump to content
gilcasaoutlook

Erro ao conectar no banco de dados MySQL com Visual Studio Xamarim

Recommended Posts

Estou tentando me conectar com um banco de dados MySQL que já utilizo em uma aplicação Visual Studio 2017 Xamarin. Quando tento conectar dá o seguinte erro

 

 

System.TypeInitializationException: The type initializer for 
'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception.    
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using System.Data;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
using System;

namespace UsandoMySql
{

public class MySQLConn
{

    MySqlConnection conexaoMySQL;
    private MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();

    public string CriaStringConexao()
    {
        builder.Server = "sql175.main-hosting.eu";
        builder.Database = "u423984934_maxe";
        builder.UserID = "u423984934_maxe";
        builder.Password = "gilcasa976431";
        builder.Port = 3306;

        return builder.ToString();
    }


    public List<string> CarregaPessoas()
    {
        List<string> lista;
        string conexao = CriaStringConexao();
        string consulta = "select nome, numero from pessoa";

        try
        {
            conexaoMySQL = new MySqlConnection(conexao);
            conexaoMySQL.Open();
        }
        catch (Exception e)
        {
            throw e;
        }


        try
        {
            MySqlDataReader dr;
            MySqlCommand cmd = new MySqlCommand(consulta, conexaoMySQL);

            dr = cmd.ExecuteReader();
            lista = new List<string>();

            while (dr.Read())
            {
                Pessoa p = new Pessoa();
                p.Nome = dr["nome"].ToString();
                p.Numero = dr["numero"].ToString();
                lista.Add(p.Nome + "                  " + p.Numero);
            }
        }
        catch (System.Exception)
        {
            throw;
        }
        return lista;
    }
}


[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    ListView listView;
    Button btnConectar;
    List<string> listPessoa;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        listView = FindViewById<ListView>(Resource.Id.listViewPessoa);

        btnConectar = FindViewById<Button>(Resource.Id.buttonConectar);
        btnConectar.Click += BtnConectar_Click;
    }

    private void BtnConectar_Click(object sender, System.EventArgs e)
    {
        MySQLConn db = new MySQLConn();
        Toast.MakeText(this, "Acesso ao MySQL feito com sucesso!", ToastLength.Short).Show();

        listPessoa = db.CarregaPessoas();

        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1, listPessoa);

    }

}
}

 

 

img1.png

img2.png

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.