sql server unicode 支援

來源:互聯網
上載者:User

所有的文檔和網上的文章都說N可以解決問題。但如果使用wstring bind後select...,則會發現得到的 wstring 格式的column很大可能結果是亂碼。

 

其實可以這樣解釋這個問題:鍵盤是沒法輸入uniocde編碼的,除非用微軟拼音的內碼輸入。因此資料庫的用戶端軟體輸入表的內容並非unicode,即使在表設計的時候用了N。因此select出來的結果並不能用wsting來解析。

 

那麼我們怎麼在初始化資料庫的時候使用unicode字串呢?

SQLRETURN SQLExecDirect(
     SQLHSTMT     {
function onclick()
{
function onclick()
{
showTip(this)
}
}
}">StatementHandle
,

     SQLCHAR *     {
function onclick()
{
function onclick()
{
showTip(this)
}
}
}">StatementText
,

     SQLINTEGER     {
function onclick()
{
function onclick()
{
showTip(this)
}
}
}">TextLength
);

sql 在執行的時候,調用SQLExecDirect,其sql語句是SQLCHAR 類型,其實就是unsigned char 的一段空間,並不一定要求這是一個以'/0'結尾的ascii 字串。如果StatementText是一個ascii 字串,TextLength可以設定為SQL_NTS。

 {
function onclick()
{
function onclick()
{
showTip(this)
}
}
}">StatementText是可以嵌入unicode字元的,比如使用INSERT INTO  T (c1,c2) VALUES(N'unicode string',data),這時候TextLength要填入整個串的長度而不是SQL_NTS。

拼字出這樣一個串既不能用窄字串的函數集合,也不能用寬字元串的函數集合。這裡使用memcpy類的函數來構造這樣的串,然後調用SQLExecDirect,取得了成功。比如,

 

USE [test]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[電話薄](
 [姓名] [ntext] NULL,
 [電話] [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 ,except 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 電話薄 (姓名,電話) values(N'";
 int len1=strlen(str1);

 wchar_t str2[]=L"張三";
 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/

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.