I'm a beginner in Xamarin
, and I'm having a hard time connecting to MySql
. I'm trying to use a simple connection just to test, but it returns an error when I click connect.
Unhandled Exception:
System.TypeInitializationException: The type initializer for 'MySql.Data.MySqlClient.Replication.ReplicationManager' threw an exception. ocorreu.
Follow my code:
namespace MySqlTeste
{
[Activity(Label = "MySqlTeste", MainLauncher = true)]
public class MainActivity : Activity
{
private Button btnConnect;
private TextView txtLog;
MySqlConnection connection;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
btnConnect = FindViewById<Button>(Resource.Id.btnCon);
txtLog = FindViewById<TextView>(Resource.Id.textLog);
btnConnect.Click += BtnConnect_Click;
}
private void BtnConnect_Click(object sender, System.EventArgs e)
{
string strConn = "server=10.0.2.2;Port=3306;database=bancoapp;userID=root;password=683122;charset=utf8";
connection = new MySqlConnection(strConn);
try
{
new I18N.West.CP1250();
connection.Open();
if (connection.State == ConnectionState.Open)
{
txtLog.Text = "Conexao Executada com Sucesso!";
}
else
{
txtLog.Text = "Conexao errada";
connection.Open();
}
}
catch(MySqlException ex)
{
txtLog.Text = ex.ToString();
}
finally
{
connection.Close();
}
}
}
The code points the error to the line that is connection.Open();