2016-05-17 73 views
0

我有一個C程序通過FreeTDS連接到MSSQL數據庫。FreeTDS:如何設置運行存儲過程的參數字符集

我試圖用設置爲類似「賽達Liberada」一個varchar參數來執行存儲過程,但它被存儲在MS SQL奇怪的字符,如上面的截圖:

Saída Liberada with wrong characters

這裏是我的freetds.conf:

# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $ 
# 
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory. 
# 
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf". 

# Global settings are overridden by those in a database 
# server specific section 
[global] 
     # TDS protocol version 
     tds version = 8.0 

     # Whether to write a TDSDUMP file for diagnostic purposes 
     # (setting this to /tmp is insecure on a multi-user system) 
     dump file = /tmp/freetds.log 
;  debug flags = 0xffff 

     # Command and connection timeouts 
;  timeout = 10 
;  connect timeout = 10 

     # If you get out-of-memory errors, it may mean that your client 
     # is trying to allocate a huge buffer for a TEXT field. 
     # Try setting 'text size' to a more reasonable limit 
     text size = 64512 

     client charset = UTF-8 

這裏是TDS的部分轉儲文件:

log.c:196:Starting log file for FreeTDS 0.91 
     on 2016-05-17 19:19:41 with debug flags 0x4fff. 
iconv.c:330:tds_iconv_open(0xaed19130, ISO-8859-1) 
iconv.c:353:Using trivial iconv 
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1 
iconv.c:187:local name for UTF-8 is UTF-8 
iconv.c:187:local name for UCS-2LE is UCS-2LE 
iconv.c:187:local name for UCS-2BE is UCS-2BE 
iconv.c:349:setting up conversions for client charset "ISO-8859-1" 
iconv.c:351:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion 
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "ISO-8859-1" conversion 
iconv.c:394:tds_iconv_open: done 

... 

net.c:741:Sending packet 
0000 01 01 00 84 00 00 00 00-20 45 58 45 43 20 55 50 |........ EXEC UP| 
0010 5f 49 4e 53 45 52 54 4d-4f 56 49 4d 45 4e 54 4f |_INSERTM OVIMENTO| 
0020 20 27 32 30 31 36 2d 30-35 2d 31 37 20 31 39 3a | '2016-0 5-17 19:| 
0030 31 39 3a 33 31 2e 30 30-30 27 2c 27 30 30 30 30 |19:31.00 0','0000| 
0040 30 30 30 30 30 30 36 34-38 33 30 35 37 30 33 30 |00000064 83057030| 
0050 27 2c 27 53 27 2c 30 2c-32 2c 27 50 43 56 43 30 |','S',0, 2,'PCVC0| 
0060 34 20 3d 20 53 61 c3 ad-64 61 20 4c 69 62 65 72 |4 = Sa.. da Liber| 
0070 61 64 61 27 2c 34 2c 27-50 43 56 43 4d 41 4e 41 |ada',4,' PCVCMANA| 
0080 47 45 52 27   -      |GER'| 

根據freetds日誌,它發送字符'í'(i銳)作爲c3廣告,這意味着它將它發送爲UTF-8(拉丁文小冊子I帶有ACUTE)。

如何設置客戶端或服務器,以便字符串可以正確存儲?

更新1:

# tsql -C 
Compile-time settings (established with the "configure" script) 
          Version: freetds v0.91 
      freetds.conf directory: /etc 
    MS db-lib source compatibility: no 
     Sybase binary compatibility: yes 
         Thread safety: yes 
         iconv library: no 
         TDS version: 4.2 
           iODBC: no 
          unixodbc: yes 
       SSPI "trusted" logins: no 
          Kerberos: no 
# 

更新2

代碼連接:

ret = SQLDriverConnect(db_msserver_dbc_handle, 
    NULL, "Driver={FreeTDS};Server=FooBar;Port=1433;Database=Foo;UID=sa;[email protected];APP=XPTO;TDS_Version=8.0;", 
    SQL_NTS, outstr, sizeof(outstr), &outstrlen, 
    SQL_DRIVER_COMPLETE); 
+0

哼,它只是一個文字的char *傳遞到的SQLBindParameter ......我反正FreeTDS會照顧它。 – natenho

+0

我更新了更多信息的問題。 – natenho

+1

嘗試將tds版本設置爲8.0請參閱http://stackoverflow.com/a/28741880/3215527 – wero

回答

1

了很多的嘗試後,我無法弄清楚,爲什麼freetds的。 conf設置(客戶端字符集和tds版本)沒有得到尊重。至少,當我追加TDS_Version = 8.0; ClientCharset = UTF-8connection string,它的工作原理!

Record is stored correctly when changed the connection string

"Driver={FreeTDS};Server=%s;Port=%s;Database=%s;UID=%s;PWD=%s;APP=%s;TDS_Version=8.0;ClientCharset=UTF-8" 

而且,freetds的的頭日誌文件被更改,提UTF-8的轉換:

log.c:196:Starting log file for FreeTDS 0.91 
     on 2016-05-18 15:58:49 with debug flags 0x4fff. 
iconv.c:330:tds_iconv_open(0xaeb19118, UTF-8) 
iconv.c:353:Using trivial iconv 
iconv.c:187:local name for ISO-8859-1 is ISO-8859-1 
iconv.c:187:local name for UTF-8 is UTF-8 
iconv.c:187:local name for UCS-2LE is UCS-2LE 
iconv.c:187:local name for UCS-2BE is UCS-2BE 
iconv.c:349:setting up conversions for client charset "UTF-8" 
iconv.c:351:preparing iconv for "UTF-8" <-> "UCS-2LE" conversion 
iconv.c:391:preparing iconv for "ISO-8859-1" <-> "UCS-2LE" conversion 
iconv.c:394:tds_iconv_open: done 
+0

連接字符串中的'TDS_Version ='是爲我修復的。謝謝! –