Share via


How to specify regional parameter when doing SQL query with C#

Question

Thursday, June 14, 2012 4:02 PM

Hello,

I have developed a software designed to migrate SQL data from a SQL Server to an other. I'm using C#. 

But I have an issue with the regional settings. When I query a table containing decimals, I get strings like "1,0000" because my computer is set up with french regional. But when I make an insert or update with the same string (1,0000), I get an error because the SQL server has US regional settings. 

I made the test of changing my laptop regional settings to US and everything is working fine. 

My question is, how can I specify the regional setting to US in my connection string ? This is the connection string I'm using so far:

SqlConnection myConnection = new SqlConnection("Data Source = IP; Initial Catalog = DB; User Id = sa; Password = pwd;");

Thank you in advance. 

All replies (7)

Wednesday, June 20, 2012 9:37 AM ✅Answered

Hi Finalflo,

Welcome to the MSDN forum.

You can try set the Regional settings for the current thread like below.

Thread.CurrentThread.CurrentCulture =new System.Globalization.CultureInfo("en-US");

You can also check this article: Custom Global Application Culture

Have a nice day.

Bob Shen [MSFT]
MSDN Community Support | Feedback to us


Wednesday, June 20, 2012 1:25 PM ✅Answered

You don't need to know the column types if they are the same in the source and destination db.


Thursday, June 14, 2012 4:12 PM | 1 vote

Connection string has nothing to do with regional settings (like language).

You will have to do some kind of conversion of data.

So how come you get strings? And another time decimals?

If field is a type of decimal, is decimal in France and in USA.

I dont get your point here, sorry.

Mitja


Thursday, June 14, 2012 4:14 PM | 1 vote

You can't do this in the connection string but have to do this on SQL Server.

http://msdn.microsoft.com/en-us/library/ms144260%28v=sql.105%29.aspx

John Grove, Senior Software Engineer http://www.digitizedschematic.com/


Thursday, June 14, 2012 4:14 PM | 1 vote

If it is a one time operation, then I suggest that chnage the computers regional settings to use US culture - Do the work - Finished.

On the other hand, why are you reading decimal values as string? use decimal values as double or decimal only. Then pass the double values directly to SQL Server.

Also, you can read the decimal values from database as string only and then use Convert.ToDecimal function to convert it into decimal value in current culture format.

decimal value = Convert.ToDecimal("1,000", new CultureInfo("de-DE"));

I hope this helps.

Please mark this post as answer if it solved your problem. Happy Programming!


Friday, June 15, 2012 6:47 AM

Don't convert the data to string and you won't get any problem with regional settings.


Friday, June 15, 2012 2:35 PM

Hello,

Thank you for your answers. 

The thing is that I have to migrate DB that have completely different structures. So I don't know the columns types ... and I think it would me pretty heavy to scan all the column and to convert the numeric data. 

Is it possible to set the Regional settings for the entire application ? With CultureInfo  maybe.