MS Access double的数字格式阻碍了ADO查询

问题描述:

您好,尊敬的CodeProject专业人士。



我创建了带编辑控件和2个按钮的对话框。



第一个按钮从MS Access 2007数据库加载双倍(比方说,例如25.45)到编辑控件。



第二个按钮存储编辑控件的值进入数据库中的同一个字段。它允许用户编辑数据库中该字段的值(例如,让我们说用户在编辑框中输入56.89)。



我使用ADO来执行以下查询:



wchar_t text [10]; //这里我存储编辑控件中的文本



GetDlgItemText(...,text,...);



//创建查询



wchar_t query [50] = LINSERT INTO MyTable(MyField)VALUES(_wtof(text));



//执行它



在Windows XP上一切正常,但在Windows 7上我得到一个错误我将通过一个例子来描述:



在Windows XP上:



我按第一个按钮。我在编辑控件中加载了25.45(注意25.45中的点!)。然后我按下第二个按钮。编辑控件的值已成功存储。



在Windows 7上:



我按第一个按钮。我在编辑控件中加载了25,45(注意25,45中的逗号!)。然后我按下第二个按钮。编辑控件的值存储为2545.



当我在Windows XP上打开数据库时,十进制值存储为25.45(再次注意25.45中的点) !)。



当我在Windows 7上打开数据库时,十进制值存储为25,45(再次注意25.45中的逗号!)。 />


似乎MS Access 2007在Windows XP上以xxx.yyy存储双倍号码,在Windows 7上以xxx,yyy存储。



转到控制面板后 - >区域和语言选项,我看到Windows 7上的小数设置使用逗号表示十进制数字,而在Windows XP上使用点。



我的问题:



如何更改字段定义以避免此行为,因此字段的格式使用如下点:xxx.yyy?



我可以用C ++ / Win32 / ADO代码来解决这个问题吗?



注意:我可以不强迫用户更改区域和语言选项,我的代码必须调整为此。



谢谢。



问候。

Hello, respected professionals from CodeProject.

I have created dialog box with edit control and 2 buttons.

First button loads double( let us say, for example 25.45 ) from MS Access 2007 database into edit control.

Second one stores the value from edit control into the same field in database. It enables the user to edit the value of that field in the database ( for example, let us say that user entered 56.89 into edit box ).

I use ADO to execute following query:

wchar_t text[10]; // here I store the text from edit control

GetDlgItemText( ..., text, ... );

// create query

wchar_t query[50] = L"INSERT INTO MyTable ( MyField ) VALUES( _wtof( text) )";

// execute it

Everything works fine on Windows XP, but on Windows 7 I get an error that I will describe bellow via an example:

On Windows XP:

I press first button . I get 25.45 ( pay attention to dot in 25.45 ! ) loaded in my edit control. Then I press second button. The value from edit control is successfully stored.

On Windows 7:

I press first button . I get 25,45( pay attention to comma in 25,45 ! ) loaded in my edit control. Then I press second button. The value from edit control is stored as 2545.

When I open the database on Windows XP, decimal value is stored as 25.45 ( again, pay attention to dot in 25.45 ! ).

When I open the database on Windows 7, decimal value is stored as 25,45 ( again, pay attention to comma in 25.45 ! ).

It seems that MS Access 2007 stores double number on Windows XP as xxx.yyy and on Windows 7 as xxx,yyy .

After going to Control Panel-> Regional and Language Options, I have seen that the settings for decimals on Windows 7 use comma for decimal numbers, and on Windows XP use dot.

My Question:

How can I change my fields definition, to avoid this behavior , so the format of the field uses dot like this : xxx.yyy ?

Is there something I can do with the C++ / Win32 / ADO code to fix this behavior ?

NOTE: I can NOT force the user to change Regional and Language Options, my code must adjust to this.

Thank you.

Regards.

编辑控件和MS Access工作正常。

他们使用小数作为正如您所注意到的那样在区域和语言选项中定义。



问题是使用 _ wtof [ ^ ]功能。



文档说明:
The edit controls and MS Access are working correctly.
They are using the decimal as defined in the Regional and Language Options as you have noticed.

The problem is the use of the _wtof[^] function.

The documentation states:
Quote:

带有_l后缀的这些函数的版本是相同的,只是它们使用传入的locale参数而不是当前的语言环境。

The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current locale.

所以 _wtof 函数使用当前的语言环境。然而,这不是您所期望的区域和语言选项中设置的那个。

来自 setlocale,_wsetlocale [ ^ ]它声明:

So the _wtof function uses the current locale. This is however not the one as set in the Regional and Language Options as you might expect.
In the documentation from setlocale, _wsetlocale[^] it states:

Quote:

在程序启动时,执行以下语句的等效语句:

At program startup, the equivalent of the following statement is executed:

setlocale( LC_ALL, "C" );

C值指定C转换的最小ANSI符合环境。 C语言环境假设所有char数据类型都是1个字节,并且它们的值总是小于256.

A value of C specifies the minimal ANSI conforming environment for C translation. The C locale assumes that all char data types are 1 byte and that their value is always less than 256.

这意味着'。'用作小数点,并且用','转换文本失败。



要解决您的问题,您需要将语言环境设置为Windows使用的语言环境。为此,您可以使用函数 _wsetlocale ,例如:

This means that the '.' is used as the decimal point and converting the text with the ',' fails.

To solve your problem you need to set the locale to the one used by windows. For this you can use the function _wsetlocale like:

_wsetlocale( LC_ALL, L"" );

或者,您可以使用 LC_NUMERIC 来仅影响小数点:

Alternatively you can use LC_NUMERIC to only effect the decimal point:

_wsetlocale( LC_NUMERIC, L"" );


我喜欢Andre的解决方案(我也给了+5)。您可以做的另一件事(如果您正在处理存储数据的多个版本,则可能需要这样做)是从数据库中获取字符串,并将所有,替换为。。在转换为double之前的字符串中。
I like Andre's solution (and I gave +5 for it too). An alternative thing you can do (and you probably need to do if you are dealing with multiple versions of the stored data) is get the string from the database, and replace all "," with "." in the string before converting to double.