正确地将数据从数据库加载到编辑控件中,以便我可以执行ADO查询 [英] Properly load data from database into edit control so I can execute ADO query
问题描述
介绍及相关信息:
我有 MS Access 2007
数据库的表格为字符串
字段,数字
一个。 数字
字段的类型为 double
,可以为负数或正数。
我使用 ADO
连接到数据库并使用<$将其数据加载到 edit
控件中c $ c> SetDialogItemText API编辑控件位于对话框中
。
用户可以编辑现有数据的值(保存更改时,我使用 GetDialogItemText
API从编辑
控件获取数据)。
问题:
一切正常在我的程序中如果用户具有英语或美国语言环境,但如果用户选择欧洲语言环境,那么我的 ADO字符串
不正确并报告错误。
这是预期的,因为在英文版中它有2个参数 - string
和 double
但是因为欧洲人使用逗号作为小数点(例如美国 123.456
是 123,456
) ADO
将其解释为具有3个参数而不是2个 - 例如:
INTRODUCTION AND RELEVANT INFORMATION:
I have an MS Access 2007
database with a table that has string
field and numerical
one. The numerical
field is of type double
and can be negative or positive.
I use ADO
to connect to the database and load its data into edit
controls with SetDialogItemText
API-edit controls are in the dialog box
.
User can edit the values of existing data ( when saving changes I get data from edit
controls with GetDialogItemText
API ).
PROBLEM:
Everything works fine in my program if the user has English or US locale, but if the user selects European locale then my ADO string
is incorrect and reports error.
This is expected since in the English version it has 2 parameters-string
and a double
but since Europeans use comma as a decimal point ( for example US 123.456
is 123,456
) ADO
interprets it as having 3 parameters instead of two-for example:
INSERT INTO table VALUES ( 'some string', 12.5 ); -- US locale
INSERT INTO table VALUES ( 'some string', 12,5 ); -- ERROR! 3 parameters instead 2
要更彻底地解释:
我们说我们有使用欧洲语言环境将数据从数据库加载到编辑
控件:
第一个持有字符串
(一些字符串
)和第二个十进制数字( 123,456
)。
我们说用户将字符串值更改为其他字符串
并按保存 按钮
。
现在我的代码从编辑
控件获取值但是必须发生错误,因为我从第二个编辑控件(记住,用户左侧小数数据未更改)得到 123,456
,并且我的 ADO字符串
配置不正确,因为它现在看起来像这样:
To explain more thoroughly:
Let us say that we have loaded data from database into edit
controls using European locale:
First one holds string
( Some string
) and second one decimal number ( 123,456
).
Let us say user changes the string value into Some other string
and presses the Save button
.
Now my code gets values from edit
controls but the error must occur since I got 123,456
from the second edit control ( remember, user left decimal data unchanged ), and my ADO string
is not properly configured since now it looks like this:
INSERT INTO table VALUES ( 'Some other string', 123,456 ); --ERROR! 3 parameters
我的问题:
如果用户设置了欧洲语言环境,是否有办法将 MS Access 2007数据库
中的小数值加载到 edit
控制用逗号更改为点?
或者我可以以某种方式子类编辑
控制将逗号更改为点?
用户输入可以限制为点和字母 sublclassing
编辑
控件,这不是问题所在。我只是想正确地从数据库加载数据,所以如果用户决定不编辑小数据,我的 ADO字符串
可以无错误地执行我上面描述的例子。
谢谢。
祝你好运。
MY QUESTION:
If the user has set the European locale, is there a way to load decimal values from MS Access 2007 database
into edit
control with comma changed into dot?
Or maybe I can somehow subclass edit
control to change comma into dot?
User input can be limited to dot and letters by sublclassing
the edit
control so that is not the problem. I just want to load data from database properly, so my ADO string
can be executed without error if user decides not to edit decimal data like in the example I described above.
Thank you.
Best regards.
推荐答案
简单。您需要将文本转换为double,然后将其插入数据库中。您还没有指定用于数据插入的内容,因此我假设您动态构建INSERT查询,如下所示(假设您使用的是MFC):
Easy. You need to convert the text to double and then insert it in the database. You have not specify what you are using for data insert, so I assume you build the INSERT query dynamically, like this (assuming you are using MFC):
CString text1, text2, sql;
GetDialogItemText(IDC_EDIT1, text1);
GetDialogItemText(IDC_EDIT2, text2);
sql.Format(_T("INSERT INTO table VALUES ( '%s', %s );"), (LPCTSTR)text1, (LPCTSTR)text2);
所以,不要这样做,请使用以下内容:
So, instead of doing this, use the following:
CString text1, text2, sql;
GetDialogItemText(IDC_EDIT1, text1);
GetDialogItemText(IDC_EDIT2, text2);
sql.Format(_T("INSERT INTO table VALUES ( '%s', %f );"), (LPCTSTR)text1, _ttof_l(text2, _get_current_locale()));
要记住的重要一点是使用atof的_l版本/ wtof函数
编辑:根据作者的建议,解决这些类型问题的最佳方法是加载当前的区域设置 _wsetlocale(LC_ALL,L);
并使用ADO参数化查询来处理所有事情。
The important thing to remember is to use "_l" versions of atof/wtof functions
As suggested by the author the best way to fix these type of problems it is to load current regional settings with _wsetlocale( LC_ALL, L"" );
and to use ADO parametrized query for everything to work.
这篇关于正确地将数据从数据库加载到编辑控件中,以便我可以执行ADO查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!