gpt4 book ai didi

c# - 安全字符串格式对象 C#

转载 作者:行者123 更新时间:2023-11-30 20:23:33 25 4
gpt4 key购买 nike

我有一段时间第一次使用 C# 工作,我有这样一行代码。

SQL = string.Format("PageName = '{0}'", bp.CommandArgument);

我需要知道如何保护对象“bp.CommandArgument”免受任何 SQL 注入(inject)攻击。谢谢。

最佳答案

为什么不用sql参数?

string commandTxt = "SELECT ... FROM ... WHERE PageName=@PageName";
var command = new SqlCommand(commandTxt, connection);
command.Parameters.Add("@PageName", bp.CommandArgument);

我假设 connection 是您声明的 SqlConnection 对象。

关于c# - 安全字符串格式对象 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28680584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com