c# - 在参数化的查询中重写没有前缀 N 和没有单引号的查询是否有区别
问题描述
我之前的查询是 name=N'hook' 然后改为 name=@name 这些更改是为了避免 sql 注入,我认为在新代码中缺少单引号和前缀 N,但我没有把握。
//OLD CODE with prefix N, the param is wrapped in single quotes
var schemaName = "";
...
var command = new SqlCommand("SELECT schema_id FROM sys.schemas WHERE name = N'" + schemaName + "')";
//OLD CODE without prefix N, the param is wrapped in single quotes
var schemaName = "";
...
var command = new SqlCommand("SELECT schema_id FROM sys.schemas WHERE name = '" + schemaName + "')";
//NEW CODE
var schemaName = "";
...
var command = new SqlCommand("SELECT schema_id FROM sys.schemas WHERE name = @schemaName");
command.Parameters.Add(new SqlParameter("@schemaName", schemaName));
解决方案
推荐阅读
- sql - Oracle DB:从数据库中不存在的列表中选择值
- python - Pandas:根据源列中的值将值写入目标列,而不覆盖目标列中的任何现有值
- kotlin - 内部类和外部类的括号是什么意思?
- python - Python Selenium:提取 Chrome 和 Firefox 浏览器的 PID
- python - Get n users from pandas dataframe by id
- google-sheets - 根据另一个工作表中的列表格式化单元格
- reactjs - 比较 2 个对象并以更智能的方式根据第一个对象调整第二个对象
- sql - 以下问题的 Informix SQL 是什么?
- java - 爪哇 | JPA | 如何将多个重复注释组合成单个注释?
- java - 使用 Java apache poi 和 React 通过 API 发送 xls