SQL Server Unicode support

Source: Internet
Author: User

All documents and online articles say n can solve the problem. However, if you select... after using wstring bind, you will find that the column in The wstring format is very likely to result in garbled characters.

 

In fact, this problem can be explained as follows: the keyboard cannot input uniocde encoding unless it is entered using an inner code of Microsoft pinyin. Therefore, the content of the input table of the database client software is not Unicode, even if n is used during table design. Therefore, the results of the SELECT statement cannot be parsed using wsting.

 

So how do we use Unicode strings when initializing the database?

SqlreturnSqlexecdirect(
Sqlhstmt{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Statementhandle
,

Sqlchar *{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Statementtext
,

Sqlinteger{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Textlength
);

When the SQL statement is executedSqlexecdirectThe SQL statement is of the sqlchar type, which is actually a space segment of the unsigned char. It is not necessarily required that this is an ASCII string ending with '/0. If statementtext is an ASCII string, textlength can be set to SQL _nt.

{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} ">StatementtextCan be embedded with Unicode characters, such as using insert into T (C1, C2) values (N 'unicode string', data). In this case, textlength must be filled with the length of the entire string instead of SQL _nt.

You cannot use a narrow string function set or a wide string function set to spell out such a string. Here, we use functions of the memcpy class to construct such a string and then callSqlexecdirect,Succeeded. For example,

 

Use [test]
Go

Set ansi_nulls on
Go
Set quoted_identifier on
Go
Create Table [DBO]. [Phone Book] (
[Name] [ntext] Null,
[Phone] [nchar] (32) null
) On [primary] textimage_on [primary]

/*

* Insert a unicode string into the table

*

*/

Struct buf_s {
Void * Buf;
Int Len;
};

 

/*

* Just be same as memcpy, should t for the return.

*/

Int memapend (void * DST, void * SRC, int Len)
{
Memcpy (DST, SRC, Len );
Return Len;
}

Void insert ()

{

Unsigned char SQL _buf [256] = {0 };
Int len_ SQL = 0;

Char str1 [] = "insert into phone book (name, phone) values (n '";
Int len1 = strlen (str1 );

Wchar_t str2 [] = l "James ";
Int len2 = wcslen (str2) * sizeof (wchar_t );

Char str3 [] = "', N '";
Int len3 = strlen (str3 );

Wchar_t str4 [] = l "010123456 ";
Int len4 = wcslen (str4) * sizeof (wchar_t );

Char str5 [] = "')";
Int len5 = strlen (str5 );

Buf_s buf_s_a [] = {str1, len1,
Str2, len2,
Str3, len3,
Str4, len4,
Str5, len5 };

For (INT I = 0; I <sizeof (buf_s_a)/sizeof (buf_s); I ++)
{
Len_ SQL + = memapend (SQL _buf + len_ SQL, buf_s_a [I]. Buf, buf_s_a [I]. Len );
}

 

Sqlexecdirect (H, SQL _buf, len_ SQL );

 

}

 

 

BTW: Find a good site for ms_sqlserver:

Http://www.functionx.com/sqlserver/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.