简体   繁体   中英

Not able to escape apostrophe/special characters in parameterized query

RESOLVED: The data was being read from a Gridview which had the html encoding. The solution was the following... Dim Sht_Text As String = HttpUtility.HtmlDecode (row.Cells.Item(0).Text)

I am trying to prevent from having to escape apostrophes and other special characters in my string variables by using a parameterized query with an OleDbConnection . I have all of the parameters in the same order, the insert is working, it just does not maintain the characters. I keep getting those pesky html codes. Any help would be appreciated. I am trying to insert into an Access db from a web pages. As I mentioned, its working, just not the special characters.

Here's the code:

Dim pConn As New OleDb.OleDbConnection pConn = New OleDbConnection(cb.ConnectionString)  
Dim SqlString As String = "INSERT INTO Strings (Mlt_String_ID, Lng_ID, Strg_Name, Sht_Text, Lng_Text, Alt_Text) Values (?, ?, ?, ?, ?, ?)"  

Using cmd As New OleDbCommand(SqlString, pConn)     
    cmd.CommandType = CommandType.Text     
    cmd.Parameters.AddWithValue("@Mlt_String_ID", Mlt_String_ID)
    cmd.Parameters.AddWithValue("@Lng_ID", Lng_ID)
    cmd.Parameters.AddWithValue("@Strg_Name", Strg_Name)
    cmd.Parameters.AddWithValue("@Sht_Text", Sht_Text)
    cmd.Parameters.AddWithValue("@Lng_Text", Lng_Text)
    cmd.Parameters.AddWithValue("@Alt_Text", Alt_Text)

    pConn.Open()
    cmd.ExecuteNonQuery()
    pConn.Close()
End Using 

Updated example: This is directly from the access table after Insert.

String_ID String_Name  Long_Text  Short_Text  Alternate_Text Multi_String_ID Lang

993       U_°C_fr °C    °C     °C        651             fr

652       U_°C_jp      °C         °C          °C             651             jp

651       U_°C         °C         °C          °C             651             en

jp and en records already existed, the insert of fr record pushes the html codes for the small circle which represents the degree symbol. Happens for all other special characters as well.

RESOLVED: The data was being read from a Gridview which had the html encoding. The solution was the following... Dim Sht_Text As String = HttpUtility.HtmlDecode(row.Cells.Item(0).Text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM