How to use the same database and the same program for two different locales in .NET.

I have a C # program that uses a SQL Server database. I already use it in a country that uses. as a decimal separator. Now I want to use it in another country, which uses it as a decimal separator.

in C # is there any application level level that I can change or write code so that I can use the same database and the same program? or do I need to change all the code to handle this new decimal separator?

I do not know how this works. I think there will be problems in My Sql Queries. For example, one of my existing statements

insert into tblproducts(productId,Price) values('A12',24.10) 

now in a new country he will become

 insert into tblproducts(productId,Price) values('A12',24,10) 

it will cause an error

so do i need to change all the code to handle this situation?

thanks

+3
source share
3 answers

If you built the query using string concatenation, use parameters instead. Therefore, instead of writing:

  var query = "insert into tblproducts(productId,Price) values('" + article + "','" + price + ')'; 

use OleDbParameters :

  var query = "insert into tblproducts(productId,Price) values(?,?)" var cmd = new OleDbCommand(query, connection); cmd.Parameters.Add("@article", OleDbType.VarChar).Value = article; cmd.Parameters.Add("@price", OleDbType.Single).Value = price; 

This will save you a lot of problems, including localization problems.

+1
source

You can do a couple of things to fix this.

firstly, if you accept values ​​from the interface, then you transfer these values ​​to decimal. Decimal.parse is a culture-dependent function and will use the current culture to analyze values. Therefore, if CurrentCulture uses commas as decimal separators, your composition will work correctly. Then, when you output a value from your variable, you can specify the decimal.ToString format for output always using a period as a separator.

Oh, forgot to add. You can also change your parsing to indicate "Currency", which allows you to enter commas as well as $ signs. e.g. decimal.parse (number, NumberStyles.Currency)

0
source

In the global.asax.vb file, you can set the culture of the current page load:

 Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US") Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture 

This will make all the functionality that supports the culture beautiful. e.g. (5000.25). ToString () will use commas or periods depending on which culture you set. In addition, reading the input from the user in a numerical type will be analyzed in accordance with their culture rules. Dates will be displayed correctly (12/9/08 compared to 9/12/08). You get it all for free.

This obviously causes problems when talking to other systems that expect everything in the same culture. To solve this problem, you write your queries using an invariant culture:

 (5000.25).ToString(CultureInfo.InvariantCulture) 

This will explicitly set this output for what Mysql can interact with.

Note. If you have the correct data layer and you pass numeric types to it, you can probably avoid this mess.

0
source

All Articles