简体   繁体   English

如何在c#中将包含单引号的字符串保存到Oracle DB

[英]How to save string containing single quotes to Oracle DB in c#

Hi.. I have to insert the below string into Oracle DB:嗨..我必须将以下字符串插入到 Oracle DB 中:

    String StrDiv = "<div STYLE=" + "'"+"font-family: " + lblPreviewPane.Font.Name + "; font-size: " + lblPreviewPane.Font.Size + "; color: " + ColorPicker1.Color + ""+"'" + ">" + lblPreviewPane.Text + "</div>";

I get the following in StrDiv:我在 StrDiv 中得到以下信息:

    "<div STYLE='font-family: Times New Roman; font-size: 8pt; color: #CC33CC'>TEXTSAMPLE</div>"

But the problem is, when i insert the string into DB,an error is thrown: ORA-00917: missing comma但问题是,当我将字符串插入 DB 时,抛出错误: ORA-00917:缺少逗号

This is because of the single quotes in the STYLE attribute in .这是因为 .

Kindly guide me with a solution for mentioned issue.请指导我解决上述问题。

You can use SQLCommand and SQLCommand.Parameters pass the StrDiv as Parameter and it should be fine您可以使用 SQLCommand 和 SQLCommand.Parameters 将 StrDiv 作为参数传递,它应该没问题

small example:小例子:

using (SqlCommand myCommand = new SqlCommand(
    "INSERT INTO table_name (column_name) VALUES (@StrDiv)")) {

    myCommand.Parameters.AddWithValue("@StrDiv", StrDiv);
    //...

    myConnection.Open();
    myCommand.ExecuteNonQuery();
    //...
}

Just use "''" to escape the "'" while inserting the data in db在数据库中插入数据时,只需使用"''"来转义"'"

String StrDiv = "<div STYLE=" + "''"+"font-family: " + lblPreviewPane.Font.Name + "; font-size: " + lblPreviewPane.Font.Size + "; color: " + ColorPicker1.Color + ""+"''" + ">" + lblPreviewPane.Text + "</div>";


"<div STYLE=''font-family: Times New Roman; font-size: 8pt; color: #CC33CC''>TEXTSAMPLE</div>"

Just replace your single quotes with twice single quote men's single quote two times.只需将您的单引号替换为两次单引号男士单引号两次即可。 use Replace("'","''") for replacing single quote with twin single quote.使用Replace("'","''")用双单引号替换单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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