Environment: VB.NET, VS 2022 (latest build), SQL Server 2019 (latest patch)
I'm attempting to execute a simple INSERT of varchar
data containing certain UTF-8 data, and regularly getting the SqlException "Invalid data for UTF8-encoded characters" when a single character is surrounded by left or right curly quotes (e.g. [left double quote]J[right double quote] -- like "J" with curly double quotes around it).
The error also occurs with curly single quotes, but DOES NOT occur with similar data not consisting of single characters not bounded in such a way (e.g. the contraction "isn[apostrophe]t" -- "isn't" with a curly apostrophe).
My code is similar to:
Dim SQL As String = "INSERT INTO MyTable ([MyField]) VALUES (@MyField)"
Dim curName As String
Using cmd As New SqlCommand(SQL, conn)
cmd.Parameters.Add("@MyField", SqlDbType.VarChar, 80)
While ...
...loop through filereader taking in UTF-8 encoded text data
curName = Await oRead.ReadLineAsync()
cmd.Parameters("@MyField").Value = curName
Try
Await cmd.ExecuteNonQueryAsync()
Catch ex As Exception
Debug.Print("Error: " & ex.Message) ' <--- Shows error here
End Try
End While
End Using
The column is set to varchar
, with the database collation set to Latin1_General_100_CI_AS_SC_UTF8.
Any idea what's going, or if a workaround is possible?