Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c# - Windows -1252 is not supported encoding name

I am working with windows 10 universal App and the ARM CPU to create apps for the Raspberry Pi. I get the following error with encoding:

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=...");
            MySqlCommand cmd;

            conn.Open();

            cmd = new MySqlCommand("Select * from ...");

            var dr = cmd.ExecuteReader();

            int count = 0;

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

            var dialog = new MessageDialog((count == 1) ? "Logged In" : "Error");
            await dialog.ShowAsync();

        }
        catch (Exception ex)
        {
           var dialog = new MessageDialog(ex.Message);
           await dialog.ShowAsync();
        }
        finally { conn.Close(); }
    }
}

I get the error in this line of code

dr = cmd.ExecuteReader();

Before I used to get it in

conn.open();

But I was able to solve it by adding

charset=utf8

to the connection string.

How can I solve this error?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From a .NET Core 2.2 project, I had to install via Nuget the following two packages:

System.Text.Encoding | CodePages

(System.Text.Encoding & System.Text.Encoding.CodePages)

Then you have to set it before the use of libraries:

 using System.Text;
 …
 {
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    ...
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...