Problems displaying/inserting special characters, MySql ASP.NET - c#

I've got problems with special characters being inserted and displayed properly. It's an ASP.NET 3.5 application (C#) and the database is MySql.
If I try to insert a special character into the database using a console app I get an error back saying 'Incorrect string value: '\x81 etc'. If I try to display them it displays for example ü instead of ü.
It works on my local machine, and the only difference I can find (and most likely the root of the problem) is that collation_server is set to latin1_swedish_ci on my machine, utf8_general_ci on the dev server, and character_set_server is latin1 on my machine, utf8 on the dev server. (Everything else is set to utf8 on both machines.)
I thought utf8 was the best thing to use, so now I'm not sure if I should change the dev server to be the same as my local machine which works, i.e. use latin, or change my local machine to be the same as the dev server and look for another solution to the problem? Any thoughts?
Thanks,
Annelie

Set your connection string as follows:
"server=<server>;port=<port>;database=<db>;uid=<uid>;pwd=<password>;charset=utf8;"
Example:
"server=localhost;port=3306;database=mydb;uid=root;pwd=root;charset=utf8;"
(add charset=utf8 to it)

Related

Blazor Server-side and Firebird Encoding issues

I am retrieving data from a Firebird database and I'm having trouble to get things to work properly.
I've set the database encoding to None and update some values with accents in my DB editor (Flamerobin or other) and store this string:
1_éöüäà
Then I load the data in my Blazor app using default Firebird client and Dapper. If I use a connection string with Charset=None encoding, the loaded string now is
1_�����
Ok so let's change the charset to Charset=ISO8859_1 and relaunch the application. I now get:
1_éöüäà
GREAT - not all is lost, it seems it is stored in DB with ISO8859_1 encoding...
So now, let's edit the value in the Blazor app and store it in the DB still with Dapper and ISO8859_1 charset. I use the SAME connection string for loading and storing data - here is what I actually save (I copy-pasted Dappers' query parameters here):
3_éöüäà
When I check what I have in my DB now (Flamerobin) I get the following strings:
3_éöüäÃ
Ok so the encoding was changed somewhere... After I reload the data in my App, I get the SAME result as above - so encoding was somehow messed up while saving and reloading from DB, using the SAME encoding?! How could that be?!
So well, why not try using Charset=None again to load the data in the app? Guess what, the result is now
3_éöüäà (in App) and 3_éöüäà(in DB)
What? Why would I see the string CORRECTLY by setting Charset=NONE in the connection string but, as it was saved with Charset=ISO8859_1 from the App?
Note that on a WINFORMS app, the exact same manip works the same whether I load/save the data with Charset=None OR Charset=ISO8859_1... Result is alwas as desired : 1_éöüäà.
EDIT: I see the SAME result on my Blazor page AND in the VS debugger console everywhere. I therefore doubt that would be a rendering issue on the browser side - it really is on the .Net / server side that things are happening!

deployed vs local: stored data are unexpected in the deployed app vs local app using mysql

I'm developing a web app and trying to store some input data from a post request using asp.net core 3.1.
at first I should say that I'm using non-English language for inputs.
In local state everything goes fine, however when I publish it and try to store data in MySQL database, values are unexpected(in form of some question marks). the first thing came to my mind was maybe I used an inappropriate charset and encoding, so I change the encoding to the closest thing that I have in my local. didn't work.
I even analyzed the HTTP request and there were no problems.
then I tried to insert directly in phpMyAdmin with insert method. worked fine.
local encoding: utf8mb4_0900_ai_ci
remote encoding: utf8mb4_unicode_ci
any help would be appreciated.
The connection parameters determine the character set of the bytes in the client. To correctly store and retrieve data you must know the charset and announce it when connecting.
While transferring data between the client and server, MySQL will covert from that charset to the charset defined for the columns you are storing into / fetching from.
It sounds like you established the charset correctly for one client and incorrectly for the other.
If you can provide the hex of the characters while in the client, I can further explain.
utf8mb4_0900_ai_ci and utf8mb4_unicode_ci are different Collations for the same character set (encoding) utf8mb4. The characters will go between client and server without any transcoding. However, comparisons may cause trouble due to the different collations.

What Is the Syntax for Including the Service Name in a Connection String?

I have a C# ASP.Net MVC web application. I am trying to successfully connect to an Oracle database.
I am getting a "ORA-12514: TNS:listener does not currently know of service requested in connect descriptor" error.
I do not have access to the server the database is on. But I do have access to Oracle SQL Developer, which I have installed on my machine.
In my C# code I am setting the connection string like this:
ConnectionString = "DataSource=XXX.XX.XXX.XXX/abcd,1521;User ID=userid;Password=password;";
abcd should be the service name. and 1521 is the port number.
I understand that my connection string might not be the cause of the error, but I want to rule it out. Also, I know the more proper way of doing things is probably to set the connection string in web.config and retrieve it as needed, but I am doing it this way just for ease of testing until I know I am able to connect to the database successfully.
What is weird to me, is that I was able to connect to the database using Oracle SQL Developer using the same IP address, port number, service name, username, and password I am using in my connection string.
Primarily, I would like help knowing if my connection string looks valid. If you have additional thoughts about what the issue could be, that would also be appreciated.
using this command in Oracle SQL Developer:
select sys_context('userenv','service_name') from dual;
I am able to determine that the service name I am using in my connection string is one that exists, although I guess this does not guarantee that the service is up.
I am not a DBA by any means. In fact, I am still new to .Net and web development in general, but I have been assigned to troubleshoot this issue. Any help is appreciated.
I don't recall seeing the following format
DataSource=XXX.XX.XXX.XXX/abcd,1521
as valid (which doesn't mean its not, I've just not seen it).
The more common ones I've seen are:
DataSource=XXX
where XXX is a reference to your tnsnames.ora file
DataSource=//nnn.nnn.nnn.nnn/service_name
DataSource=//nnn.nnn.nnn.nnn:port/service_name
So maybe try those variants and see how you go. There's also more definitive list of alternatives at https://www.c-sharpcorner.com/UploadFile/nipuntomar/connection-strings-for-oracle/
I ended up figuring this out. My connection string format I don't t think was correct. I changed it to be:
ConnectionString = "DataSource=XXX.XX.XXX.XXX/abcd;User ID=userid;Password=password;";
Basically, I just took off the port number. In my case, the default port was what I needed anyway. Not sure what I would have done had I needed to specify the port number.
As new to Oracle I struggled a few days finding solution to this
this article helped me alot
As of Oracle 21 c
This is my Connection string for C#
Password=dev;Persist Security Info=True;User ID=Dev;Data Source=localhost:1521/XEPDB1
Keep in mind Dev is Username which is also the Schema name for Oracle

Gibberish data using npgsql from windows using utf8.en_us encode

I developed a .net program to retrieve data through an api and import the data into postgresql database with utf8 encode hosted on AWS RDS.
The problem is that I developed the code on a windows machine with gb2312(active page code: 936) but I deployed the program on a windows server with utf8.en_us(active page code:437) on AWS. The application running on my machine can correctly store Chinese characters into database and displays well. But it turns out to import Gibberish data into database when running on server.
I tried to do a conversion directly on string within the code, like this:
private string Utf8y(string a)
{
return Encoding.UTF8.GetString(Encoding.Default.GetBytes(a));
}
But it's in vain.
Any idea or solution on this?
Thank you!
Finally find out a solution:
Go to control panel -> language, time, location blabla, change your non-unicode encoding to Chinese(Simplified).
Should work with other languages too.

I want to run C# program without changing SQL Server instance or without changing SQL Server installed PC name

I have C# application which uses SQL Server R2 as its database. That database is on a separate PC by the name of SERVER. SQL Server's instance name also server.
My windows application uses DataSet to communicate with the database. Now my SQL installed PC name is change to another name, ex SERVERHP. Now all my coding works want to change my connection string. Are there any other easy way to do it ?
I am to tried to edit hosts file, but it does not work for me.
This is my coding style (http://goo.gl/FQrkp). I am using DataSet with DataAdapter with IDE designers.
I have 100 ~ 150 forms. Now I cannot compile all codings. I want to easy method to connect that SQL Server database.
I want to have a way to hide the change of the hostname of the computer
You have a couple of options:
Globally replace all server names with IP address in connection strings in the app
Globally replace all server names with the new server name in the app
Add a CNAME record to the DNS table on the server (assuming of course you're in the same network, which you are if you're using computer names)
Add an entry to the LMHOSTS file (you can add as many names as you'd like that point to the same IP)
As I understand, it is SqlConnection used to connect to database, or something like that. Why don't you use SqlConnectionStringBuilder? Then you can dinamically construct connection string you need. Also, to get list of servers, you can use SqlDataSourceEnumerator, from namespace System.Data.Sql.
It sounds like you want to have a way to hide the change of the hostname of the computer (Server/Desktop/Virtualized instance of Windows, whatever) that is running your SQL Server.
This isn't my area of expertise, but I can't think of a way to do it that only involves your application code and just the computer.
If you control the local DNS you can create a CNAME entry with the old name that "points" at the new one. Depending on how your connection strings are stored, you might have to edit them or you might not. But you won't have to worry about the location of your SQL Server changing again because you can always edit your CNAME to point at the new location.
Note for the future - not your current problem: If you continue to use MS SQL Server in particular you'll want to be careful about moving it to a computer where it isn't the default instance because then you need to put the instance name in the connection string as well, which might force you to edit all of your application web.configs and app.configs again.
Are you using Visual Studio 2012 with your development efforts? I ran into a conflict with the mini SQL db VS installs (Using Premium version) and had to modify the ConnectionStrings section of my Machine.config file to point to my SQL database. For whatever reason, VS will write references to the mini db in the Machine.Config file (for whatever version of .Net you are leveraging) throwing potetial conflicts.
The file can be found in %systemroot%\Microsoft.Net\Framework64\dot net version\Config
If you're using an x86 processor the Framework64 folder is just called 'Framework'.

Categories

Resources