microsoft / go-mssqldb

Microsoft SQL server driver written in go language
BSD 3-Clause "New" or "Revised" License
283 stars 63 forks source link

BulkCopy and chinese characters #220

Open Flashcqxg opened 2 weeks ago

Flashcqxg commented 2 weeks ago

Using BulkCopy to insert Chinese data into the table in batches can be successfully stored in the database,however, the Chinese characters stored in the database are garbled. For example, the string "你好" becomes "浣犲ソ " in the database. The type of the field is varchar, and according to requirements, nvarchar cannot be used instead. Please tell me how to solve this requirement, thank you.

stmt, err := txn.Prepare(mssql.CopyIn("t_batch_update", mssql.BulkOptions{}, "id", "name"))
if err != nil {
    fmt.Println(err)
    return
}
for i := 0; i < 10; i++ {
    _, err = stmt.Exec(i, "你好")
    if err != nil {
        fmt.Println(err)
        return
    }
}
result, err := stmt.Exec()
if err != nil {
    fmt.Println(err)
    return
}
err = stmt.Close()
if err != nil {
    fmt.Println(err)
    return
}
err = txn.Commit()
if err != nil {
    fmt.Println(err)
    return
}