Open travelliu opened 2 months ago
thx for opening an issue!
@travelliu does the insert statement put the right string into the database column, and it's just being scanned incorrectly into the []byte
slice?
The driver sends over all Go strings as UTF16, so the engine is going to convert them to whatever the collation of your column is. I think you need to specify a Chinese collation like Chinese_Simplified_Pinyin_100_CI_AI on the column itself, then you can send over regular Go strings with no need for explicit GBK or ISO encoding.
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", server, user, password, port)
conn, err := sql.Open("mssql", connString)
_,err = conn.Exec("drop table if exists dbo.yp_supplier")
_,err = conn.Exec("create table dbo.yp_supplier(supplier_code bigint, supplier_name char(100) collate Chinese_Simplified_Pinyin_100_CI_AI)")
_,err = conn.Exec("insert into dbo.yp_supplier(supplier_code, supplier_name ) values(7, '福建九州通譚嵄医疗器械有限公司泉州分公司')")
rows, err := conn.Query(
"SELECT supplier_name from dbo.yp_supplier",
)
defer rows.Close()
for rows.Next() {
var supplier_name string
if err = rows.Scan(
&supplier_name,
); err != nil {
log.Fatal(err)
return
}
}
@shueybubbles It's a historical problem, and now it's about migrating the data. But find out the wrong data. The main problem is CharsetToUTF8. Is it possible to provide a parameter that does not automatically convert to UTF8, and use the transcoding process by yourself after getting the data, or provide a function to register the transcoding method?
like pyodbc https://github.com/mkleehammer/pyodbc/wiki/Unicode#microsoft-sql-server
i think there are separate issues here. For one, the collation of the column in the database should match the data. Otherwise, performance is going to be poor and queries from any driver risk getting back bad data. Two, if you just want to read the raw bytes from the column you can cast it to a binary type in the query, like
select convert(varbinary(max),'abc') as binarychar
binarychar
0x616263
Then you can decode the bytes on the client however you want.
Lastly, go-mssqldb does have a VarChar
type you can use to insert data. It just converts the parameter value to a []byte
and passes it along to the server.
Describe the bug insert into some chinese code return data error
To Reproduce
Expected behavior A clear and concise description of what you expected to happen.
Further technical details
SQL Server version: (e.g. SQL Server 2017) Operating system: (e.g. Windows 2019, Ubuntu 18.04, macOS 10.13, Docker container) Table schema
Additional context Add any other context about the problem here.