撇号导致查询问题

Apostrophe causes a query issue

当我对 table 的人口执行查询时,我只出现了两次错误。特别是,当名称字段具有如下值时:

K'Ogladbach

现在撇号引起了问题,因为查询将此命令解释为新值,这是错误的。 这是我在 SQL:

中的查询结构
string sql = @"insert into Team (name, code, shortName, squadMarketValue, 
                    crestUrl, link_self, link_fixtures, link_players, caption) 
                    values ('" + item.name + "', '" + item.code + "', '" +
                    item.shortName + "', '" + item.squadMarketValue + "', '" +
                    item.crestUrl + "', '" + item._links.self.href + "', '" +
                    item._links.fixtures.href + "', '" + item._links.players.href + "', '" + 
                    campionato + "')";

如何查看 ' value ' 表示的每个新值, 因此,如果值中有 " ' ",这是一个问题,因为查询失败了 table 人口。希望有解决办法。

使用 SqlParameterized 查询而不是原始查询 SQL。这将有助于防止 SQL 注入并提供可靠的解决方案。

SqlCommand command = new SqlCommand("INSERT INTO Team (name) VALUES (@name)", con);

command.Parameters.Add("@name", SqlDbType.NVarChar).Value = item.name;
// Add the rest of the parameters here

command.ExecuteNonQuery();  // Execute the command

SqlParameterized 查询是最好的方法

您还可以将数据中的 ' 替换为 ''
那不是双引号——它是两个单引号
例如。 K'Ogladbach 到 K''Ogladbach