如果我尝试 SELECT 某些数据,我会收到此错误:'windows-1252' 不是受支持的编码名称

I get this Error if i try to SELECT some data: 'windows-1252' is not a supported encoding name

我正在尝试使用 MySql 数据库构建一个 Windows 通用应用程序。我的问题是,当我尝试执行 SELECT 命令时,出现此错误: 'windows-1252' 不是受支持的编码名称。'

我正在使用 MySQL Connector Net 6.9.8 和 Visual Studio 2015。

有一个真实的 post 和我有完全相同的问题: Windows -1252 is not supported encoding name. C#

但我是通用应用程序的初学者。在我的研究过程中,我还发现了这个 post: Reading Windows-1252 encoding in Windows Phone 8,在第一个命令中,手动编码器有 al link。 但正如我告诉过你的那样,我是一个初学者,所以我没有得到解决方案,也不知道如何将它用于我的应用程序。 请为初学者解释;)

编辑: 起初我用 INSERT 命令遇到了同样的问题,但我可以通过在连接中设置 charset=utf8 来修复它。但这不适用于 SELECT 命令。

这是我的应用程序代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using SQLite;
using MySql;
using MySql.Data.MySqlClient;

public sealed partial class MainPage : Page
{

    public MainPage()
    {
        this.InitializeComponent();
    }


    #region ++++++++MySql++++++++
    static string server = "127.0.0.1";
    static string database = "test";
    static string user = "root";
    static string pswd = "";
    MySqlConnection connectionMySql;
    int id = 1;

    private void MySqlConnectButton_Click(object sender, RoutedEventArgs e)
    {
        MySqlStatusTextBlock.Text = "Connecting...";
        string connectionString = "Server = " + server + ";database = " + database + ";uid = " + user + ";password = " + pswd + ";SslMode=None;charset=utf8";
        connectionMySql = new MySqlConnection(connectionString);
        connectionMySql.Open();
        MySqlStatusTextBlock.Text = "Connected";
    }

    private void MySqlAddButton_Click(object sender, RoutedEventArgs e)
    {
        Foods food = new Foods() { Name = MySqlElementA.Text, Keyword = MySqlElementB.Text, Calories = int.Parse(MySqlElementC.Text), Fat = double.Parse(MySqlElementD.Text), Carbs = 33.4, Protein = 24.8 };

        string para = "('" + id + "','" + food.Name + "','" + food.Keyword + "','" + food.Calories + "','" + food.Fat + "','" + food.Carbs + "', '" + food.Protein + "') ";
        string commandMySql = "INSERT INTO `foods`(`id`, `name`, `keyword`, `calories`, `fat`, `carbs`, `protein`) VALUES" + para;
        MySqlCommand cmd = new MySqlCommand(commandMySql, connectionMySql);

        MySqlDataReader dr;
        dr = cmd.ExecuteReader();

        int count = 0;
        while (dr.Read())
            count += 1;
        if (count == 0)
        {
            MySqlStatusTextBlock.Text = "Added";
        }
        else
        {
            MySqlStatusTextBlock.Text = "Error";
        }
        dr.Close();

    }

    private void MySqlShowAllButton_Click(object sender, RoutedEventArgs e)
   {
        List<Foods> f_list = new List<Foods>();
        string query = "SELECT * FROM `foods` WHERE 1 ";
        MySqlCommand cmd = new MySqlCommand(query, connectionMySql);

        MySqlDataReader dr;
        dr = cmd.ExecuteReader(); **<--- Here occurs the error**

        while (dr.Read())
        {
            Foods f_get = new Foods() {ID = dr.GetInt32("id"), Name = dr.GetString("name"), Keyword =dr.GetString("keyword"), Calories = dr.GetInt32("calories"), Fat = dr.GetDouble("fat"), Carbs = dr.GetDouble("cabrs"), Protein = dr.GetDouble("protein")};
            f_list.Add(f_get);
        }
        string textbox = String.Empty;
        foreach(Foods f in f_list)
        {
            textbox = String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}", f.ID.ToString(), f.Name, f.Keyword, f.Calories, f.Fat, f.Carbs, f.Protein) + System.Environment.NewLine;
        }
        MySqlResultTextBlock.Text = textbox;

    }
    #endregion


}

您可以在数据库中进行更改,而不是在客户端。
检查你的数据库 collation
您可以尝试转换数据库和 table collaction with:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

PS: 转换前不要忘记备份

我没有将排序规则设置为 utf8,

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

但是在您的 link Collation 中,第一个命令让它起作用。 非常感谢!我是这样修复的:

If you use PHP with mysql you may want to use in my.ini: skip-character-set-client-handshake with:

collation_server=utf8_unicode_ci
character_set_server=utf8