如何在数据库中插入撇号? [英] How to insert an apostrophe in database?

查看:154
本文介绍了如何在数据库中插入撇号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的数据库中插入一个撇号,但它不能正常工作



插入供应商(公司)

值(' ABC的'''供应')

I want to insert an apostrophe in my database but it is not working

insert into supplier(company)
Values('ABC's'' 'Supply')

推荐答案

不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



它解决了你的问题,也避免了SQL注入攻击:

Don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

It gets rid of your problem, and also avoids SQL Injection attack:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO supplier (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        com.Parameters.AddWithValue("@C1", "ABC's");
        com.Parameters.AddWithValue("@C2", "Supply");
        com.ExecuteNonQuery();
        }
    }

如果您尝试直接在SQL中执行此操作,那么将单引号加倍将允许您将其插入固定文本字符串:

If you are trying to do this directly in SQL, then doubling up your single quotes will allow you to insert it for a fixed text string:

INSERT INTO supplier (myColumn1, myColumn2) VALUES ('ABC''s', 'Supply')


这篇关于如何在数据库中插入撇号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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