Libre Base宏向SQL Server插入波兰字符显示为?的解决求助
问题描述
使用Libre Base通过ODBC驱动连接SQL Server数据库,手动插入波兰特殊字符(ą、ę、ż、ź、ó等)时显示正常,但通过Libre Basic宏读取XSLT文件插入数据时,这些字符全部变成?。
已知情况:
- 宏中用MsgBox能正确显示读取到的波兰字符
- 已尝试
StrConv(TextVariable, vbUnicode)/vbFromUnicode,无效 - SQL Server排序规则为
Polish_CI_AS - 宏连接时尝试过
CP1250、Windows-1250、UTF8等多种CharacterSet参数,均无效 - 宏连接代码:
oConnection = oDriverManager.getConnectionWithInfo(sUrl, Array( _ MakeProperty("User", sUser), _ MakeProperty("Password", sPassword), _ MakeProperty("CharacterSet", "CP1250") ))
解决方案
1. 使用参数化查询代替字符串拼接
直接拼接SQL字符串会导致编码转换异常,改用参数化查询让驱动自动处理字符编码:
Dim oStmt As Object Dim sSQL As String ' 替换为你的表名和字段名 sSQL = "INSERT INTO YourTargetTable (PolishTextColumn) VALUES (?)" oStmt = oConnection.prepareStatement(sSQL) ' 将读取到的文本变量传入参数 oStmt.setString(1, TextVariable) oStmt.executeUpdate() oStmt.close()
2. 调整ODBC连接的AutoTranslate参数
SQL Server ODBC驱动默认的自动字符集转换可能和Libre Base的设置冲突,添加AutoTranslate=False参数:
oConnection = oDriverManager.getConnectionWithInfo(sUrl, Array( _ MakeProperty("User", sUser), _ MakeProperty("Password", sPassword), _ MakeProperty("CharacterSet", "Windows-1250"), _ MakeProperty("AutoTranslate", "False") ))
3. 确认XSLT文件的读取编码
确保读取XSLT文件时使用了正确的编码(比如文件实际是UTF-8保存的,就用UTF-8解码):
Dim oFile As Object Dim oTextStream As Object oFile = CreateObject("Scripting.FileSystemObject") ' 第三个参数False表示不创建新文件,第四个参数-1代表UTF-8编码 oTextStream = oFile.OpenTextFile("your_file_path.xslt", 1, False, -1) TextVariable = oTextStream.ReadAll() oTextStream.Close()
4. 移除宏连接中的CharacterSet参数
手动连接时字符集设置正常,尝试让宏继承ODBC数据源的默认配置,删除连接代码中的CharacterSet参数:
oConnection = oDriverManager.getConnectionWithInfo(sUrl, Array( _ MakeProperty("User", sUser), _ MakeProperty("Password", sPassword) ))
内容的提问来源于stack exchange,提问作者B_K
相关产品推荐
相关产品推荐

