alexbrainman / odbc

odbc driver written in go
BSD 3-Clause "New" or "Revised" License
352 stars 140 forks source link

Chinese garbled when SQL2000 data column type is VARCHAR #115

Closed dotqi closed 6 years ago

dotqi commented 6 years ago

When the SQL2000 data column type is VARCHAR, save the Chinese language and the data extracted from general QUERY. How do we deal with this problem? Display block SRC DATA "a刘9" GET DATA a��9

alexbrainman commented 6 years ago

Please, provide program that you run and show program output (garbled). Show output as picture here.

Thank you.

Alex

dotqi commented 6 years ago
db,err := sql.Open("odbc", "driver={sql server};server=127.0.0.1;port=1433;uid=sa;pwd=imissyou;database=HN_NCMS")
if err!=nil {
    fmt.Println(err)
    return
}
sqlCode := "select convert(varchar(200),'9你好8') as fvr,convert(nvarchar(200),'9你好8') as fnr"
rows,err := db.Query(sqlCode)
if err!=nil {
    fmt.Println(err)
} else {
    var fvr,fnr string
    for rows.Next() {
        err = rows.Scan(&fvr,&fnr)
        fmt.Println(fvr,fnr)
    }       
    rows.Close()
}
<-ticks
db.Close()

VALUES : "9����8 9你好8"

alexbrainman commented 6 years ago

@admin87 I pretty sure that varchar is for storing ASCII characters - you cannot store Chinese in varchar. You need nvarchar to be able to store Chinese.

Alex

dotqi commented 6 years ago

You're right to explain. Can there be a solution?

alexbrainman commented 6 years ago

Can there be a solution?

I do not know what your particular problem is. But varchar only allows for maximum of 256 characters. That is not enough to encode Chinese characters. I would use nvarchar to store Chinese.

Alex

dotqi commented 6 years ago

Thank you very much. It can be closed. I am thinking about it myself.