如何将带有 ( ' ) 的字符串插入到 sql 数据库中? [英] How to insert a string with ( ' ) in to the sql database?

查看:68
本文介绍了如何将带有 ( ' ) 的字符串插入到 sql 数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有由 ( ' ) 引号组成的字符串,如母爱"......

I have the strings which consists of ( ' ) quote mark like "mother's love" ...

在通过 c# 中的 sql 查询插入数据时.它显示错误.我该如何解决问题并成功插入此类数据?

While inserting the data by sql query from c#. It shows error. How can i rectify the problem and insert this kind of data successfully?

string str2 = "Insert into tblDesEmpOthDetails (EmpID, Interviewnotes) values ('" + EmpId + "','" + Interviewnotes + "')";

面试笔记包含母爱"之类的值(带单引号).执行此查询时,它显示错误为字符串') 后的未关闭引号",我该如何插入这种类型的字符串?

Interview notes consists the value like "Mother's love" (with single quote). While executing this query it shows error as "Unclosed quotation mark after the character string ')" how can i insert this type of strings?

推荐答案

我很确定你没有使用 SQL 参数:

I'm pretty sure you don't use SQL parameters:

using (SqlCommand myCommand = new SqlCommand(
    "INSERT INTO table (text1, text2) VALUES (@text1, @text2)")) {

    myCommand.Parameters.AddWithValue("@text1", "mother's love");
    myCommand.Parameters.AddWithValue("@text2", "father's love");
    //...

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

这篇关于如何将带有 ( ' ) 的字符串插入到 sql 数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆