如何修复Connection String错误? con.Open()抛出'MySql.Data.MySqlClient.Replication.ReplicationManager'

时间:2019-08-25 23:17:51

标签: c# android mysql visual-studio xamarin

我正在学习通过以下链接建立与本地Mysql数据库的连接: https://www.youtube.com/watch?v=FOZ8HNJMXXg&list=WL&index=23&t=920s 我得到一个错误,它说, “未处理的异常: System.TypeInitializationException:'MySql.Data.MySqlClient.Replication.ReplicationManager'的类型初始值设定项引发了异常。发生”。

我尝试安装Mysql.Data,MysqlConnector,Xamarin.Mysql.Data。 无法解决我的问题

我试图从此链接将Mysql.Data切换到Connector, ReplicationManager threw an exception on opening a connection 仍然无法解决并显示新错误,提示“尝试添加引用/安装nuget包以引用System.Memory ...”

我也搜索过Google,但无法解决此错误。

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;

namespace App6
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private EditText _username;
        private EditText _password;
        private Button _insert;
        private TextView _syslog;  

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

            _username = FindViewById<EditText>(Resource.Id.username);
            _password = FindViewById<EditText>(Resource.Id.password);
            _insert = FindViewById<Button>(Resource.Id.insert);
            _syslog = FindViewById<TextView>(Resource.Id.syslog);

            _insert.Click += _insert_Click;
        }

        private void _insert_Click(object sender, System.EventArgs e)
        {
            //string connString = "Server = db4free.net; Port = 3306; database = db_nyube; User Id = naratama; Password = naratama123; charset = utf8";
            //MySqlConnection conn = new MySqlConnection(connString);
            //MySqlConnection conn = new MySqlConnection("Server=localhost;Port=3306;database=db_nyube;User Id=root;");
            //MySqlConnection conn = new MySqlConnection("server=db4free.net;user=naratama;database=db_nyube;port=3306;password=naratama123");
            string conStr = "Server = localhost; Database = db_nyube;  Uid = root; Pwd =;";
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = conStr;
            //MySqlConnection conn = new MySqlConnection("Host = 127.0.0.1; UserName = root; Password =; Database = db_nyube;");
            try
            {
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                    _syslog.Text = "Sukses";
                }
            }
            catch (MySqlException ex)
            {
                _syslog.Text = ex.ToString();
            }
            finally
            {
                conn.Close();
            }                  
        }
    }
}

i使用Visual Studio 2017最新版本(15.9.15)以及Microsoft .NET Framework 4.7.03056

请帮助我解决此错误。

1 个答案:

答案 0 :(得分:1)

我只是将Mysql.Data更改为Mysql.Connector,如果Mysql.Connector错误“对System.Memory的引用出现”,只需从此链接安装最新的System.Memory:https://www.nuget.org/packages/System.Memory/

感谢@BradleyGrainger