long8
asked on
How to add Unicode strings to database?
[Env: Win2k AS, SQL Server 7.0]
Hi,
My database has field of 'ntext' type. I want to somehow programmatically add to it data continaing
Unicode chars, for example Japanese. How can I do this? Using ODBC? May be someone can give a sample?
Thanks in advance!
Hi,
My database has field of 'ntext' type. I want to somehow programmatically add to it data continaing
Unicode chars, for example Japanese. How can I do this? Using ODBC? May be someone can give a sample?
Thanks in advance!
ASKER
ADO. Actually it does not matter. If you can give me working ODBC and OLEDB code I will accept your answer.
I'm now trying parameterized quiries in ADO...
Also I'm using sql server 2000, not 7.0, I mistaken.
I'm now trying parameterized quiries in ADO...
Also I'm using sql server 2000, not 7.0, I mistaken.
THe thing is, you really don't need to do anything if you are using ADO. As a COM object, it sjould work in UNICODE as its native tounge,\. One needs to take pains to program in ANSI.
When you assign a value to an ntext field, just make sure that you assign a UNICODE string. What happens when you do code like this:
#define RsITEM(rs,x) rs->Fields->Item[_variant_ t(x)]->Val ue
spRS->Open(... )
spRS->RsITEM("LastName")= _bstr_t(L"Jones");
-- Dan
When you assign a value to an ntext field, just make sure that you assign a UNICODE string. What happens when you do code like this:
#define RsITEM(rs,x) rs->Fields->Item[_variant_
spRS->Open(... )
spRS->RsITEM("LastName")= _bstr_t(L"Jones");
-- Dan
ASKER
//I have UTF-8 Japanese string. I'm converting it to UNICODE:
int nTextLen=MultiByteToWideCh ar(CP_UTF8 ,0,"タテ スカï¾ï ¾…ニï¾ï¾ „シト",- 1,NULL,0);
wchar_t *wszText=new wchar_t[nTextLen+1];
MultiByteToWideChar(CP_UTF 8,0,"ï¾€ï¾ ƒï½½ï½¶ï¾ ナニï¾ï ¾„シト", -1,wszText ,nTextLen) ;
//Then I convert string to BCB's analogue of BSTR:
//C++Builder implements the WideString type as a class.
//WideString represents a string of wide characters
//(wchar_t). As such, it is useful when working with COM.
//WideString uses BSTRs in its underlying implementation. In
//addition to the usual string operators, the class includes
//methods for converting to or from BSTR values as well as
//for sharing the value of an existing BSTR.
WideString ws = WideString(wszText);
// Here is the code performing adding new row to database
ADOConnection1->Open("sa", "");
ADOQuery1->Connection = ADOConnection1;
ADOQuery1->SQL->Text = "INSERT INTO [MRATestProduct].[dbo].[Pr Track1] VALUES (4, :Description";
ADOQuery1->Parameters->Par amByName(" Descriptio n")->Value = ws;
ADOQuery1->ExecSQL();
ADOQuery1->Connection = NULL;
ADOConnection1->Close();
Resulting row in database contains quotation marks ('?') instead of Japanese chars.
When I'm setting parameter value as bstr, like this:
ADOQuery1->Parameters->Par amByName(" Descriptio n")->Value = ws->c_bstr();
I see the following error message: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1:Incorrect syntax near '@P1'.
int nTextLen=MultiByteToWideCh
wchar_t *wszText=new wchar_t[nTextLen+1];
MultiByteToWideChar(CP_UTF
//Then I convert string to BCB's analogue of BSTR:
//C++Builder implements the WideString type as a class.
//WideString represents a string of wide characters
//(wchar_t). As such, it is useful when working with COM.
//WideString uses BSTRs in its underlying implementation. In
//addition to the usual string operators, the class includes
//methods for converting to or from BSTR values as well as
//for sharing the value of an existing BSTR.
WideString ws = WideString(wszText);
// Here is the code performing adding new row to database
ADOConnection1->Open("sa",
ADOQuery1->Connection = ADOConnection1;
ADOQuery1->SQL->Text = "INSERT INTO [MRATestProduct].[dbo].[Pr
ADOQuery1->Parameters->Par
ADOQuery1->ExecSQL();
ADOQuery1->Connection = NULL;
ADOConnection1->Close();
Resulting row in database contains quotation marks ('?') instead of Japanese chars.
When I'm setting parameter value as bstr, like this:
ADOQuery1->Parameters->Par
I see the following error message: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1:Incorrect syntax near '@P1'.
ASKER
//I have UTF-8 Japanese string. I'm converting it to UNICODE:
int nTextLen=MultiByteToWideCh ar(CP_UTF8 ,0,"タテ スカï¾ï ¾…ニï¾ï¾ „シト",- 1,NULL,0);
wchar_t *wszText=new wchar_t[nTextLen+1];
MultiByteToWideChar(CP_UTF 8,0,"ï¾€ï¾ ƒï½½ï½¶ï¾ ナニï¾ï ¾„シト", -1,wszText ,nTextLen) ;
//Then I convert string to BCB's analogue of BSTR:
//C++Builder implements the WideString type as a class.
//WideString represents a string of wide characters
//(wchar_t). As such, it is useful when working with COM.
//WideString uses BSTRs in its underlying implementation. In
//addition to the usual string operators, the class includes
//methods for converting to or from BSTR values as well as
//for sharing the value of an existing BSTR.
WideString ws = WideString(wszText);
// Here is the code performing adding new row to database
ADOConnection1->Open("sa", "");
ADOQuery1->Connection = ADOConnection1;
ADOQuery1->SQL->Text = "INSERT INTO [MRATestProduct].[dbo].[Pr Track1] VALUES (4, :Description";
ADOQuery1->Parameters->Par amByName(" Descriptio n")->Value = ws;
ADOQuery1->ExecSQL();
ADOQuery1->Connection = NULL;
ADOConnection1->Close();
Resulting row in database contains quotation marks ('?') instead of Japanese chars.
When I'm setting parameter value as bstr, like this:
ADOQuery1->Parameters->Par amByName(" Descriptio n")->Value = ws->c_bstr();
I see the following error message: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1:Incorrect syntax near '@P1'.
int nTextLen=MultiByteToWideCh
wchar_t *wszText=new wchar_t[nTextLen+1];
MultiByteToWideChar(CP_UTF
//Then I convert string to BCB's analogue of BSTR:
//C++Builder implements the WideString type as a class.
//WideString represents a string of wide characters
//(wchar_t). As such, it is useful when working with COM.
//WideString uses BSTRs in its underlying implementation. In
//addition to the usual string operators, the class includes
//methods for converting to or from BSTR values as well as
//for sharing the value of an existing BSTR.
WideString ws = WideString(wszText);
// Here is the code performing adding new row to database
ADOConnection1->Open("sa",
ADOQuery1->Connection = ADOConnection1;
ADOQuery1->SQL->Text = "INSERT INTO [MRATestProduct].[dbo].[Pr
ADOQuery1->Parameters->Par
ADOQuery1->ExecSQL();
ADOQuery1->Connection = NULL;
ADOConnection1->Close();
Resulting row in database contains quotation marks ('?') instead of Japanese chars.
When I'm setting parameter value as bstr, like this:
ADOQuery1->Parameters->Par
I see the following error message: [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1:Incorrect syntax near '@P1'.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi long8,
Do you have any additional questions? Do any comments need clarification?
-- Dan
Do you have any additional questions? Do any comments need clarification?
-- Dan
ASKER
Well, I've got it working using parameterized queries and your example! Great thanks!
-- Dan