加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Windows -1252不支持编码名称. C#

发布时间:2020-12-14 04:15:56 所属栏目:Windows 来源:网络整理
导读:我正在使用 Windows 10 Universal App和ARM CPU为Raspberry Pi创建应用程序.编码时出现以下错误: Additional information: ‘windows-1252’ is not a supported encoding name. For information on defining a custom encoding,see the documentation for
我正在使用 Windows 10 Universal App和ARM CPU为Raspberry Pi创建应用程序.编码时出现以下错误:

Additional information: ‘windows-1252’ is not a supported encoding name. For information on defining a custom encoding,see the documentation for the Encoding.RegisterProvider method.

这是我的代码.

private async void Login(string passcode)
    {
        try
        {
            MySqlConnection conn = new MySqlConnection("Server=127.0.0.1;Port=3306;Database=database;Uid=username;Pwd=password;SslMode=None;charset=utf8");
            MySqlCommand cmd;

            conn.Open();

            cmd = new MySqlCommand("Select * from users where User = '" + passcode + "'",conn);

            MySqlDataReader dr;

            dr = cmd.ExecuteReader();

            int count = 0;

            while (dr.Read())
            {
                count += 1;
            }

            if(count == 1)
            {
                var dialog = new MessageDialog("Logged In");
                await dialog.ShowAsync();
            }
            else
            {
                var dialog = new MessageDialog("Error");
                await dialog.ShowAsync();
            }

        }
        catch (Exception ex)
        {
            if (ex is MySqlException)
            {
                MySqlException exl = (MySqlException)ex;
                var dialog = new MessageDialog(ex.Message + Environment.NewLine + exl.Number);
                await dialog.ShowAsync();
            }
            else
            {
                var dialog = new MessageDialog(ex.Message + Environment.NewLine);
                await dialog.ShowAsync();
            }

            //throw;
        }
        finally
        {
            conn.Close();
        }


    }
}

我在这行代码中得到了错误

dr = cmd.ExecuteReader();

在我习惯之前

conn.open();

但我能够通过添加来解决它

charset=utf8

到连接字符串.

我该如何解决这个错误?

我解决了这个问题
System.Text.EncodingProvider ppp;
ppp = System.Text.CodePagesEncodingProvider.Instance;
Encoding.RegisterProvider(ppp);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读