我如何将字符串转换成安全的SQL字符串? [英] How do I convert a string into safe SQL String?

查看:167
本文介绍了我如何将字符串转换成安全的SQL字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一堆文本文件生成某些SQL INSERT语句。

I'm generating some sql insert statements from a bunch of text files.

这些文本文件通常是用户输入数据。我想清理此数据,以便它不会打破插入语句。

These text files are generally user input data. I would like to sanitize this data so that it's not going to break the insert statement.

例如,一些输入数据,人们用这个词不要。而',在不将导致SQL语句认为字符串已经结束,因此会导致错误。

For example, some of the input data, people have used the word Don't. The "'" in don't will lead the sql statement to think the string has ended and therefore cause an error.

有任何.NET方法,我可以打电话那种把所有这些人物要么逃避codeS或安全角色?

Is there any .NET method I can call to kind of convert all of these characters to either escape codes or safe characters?

推荐答案

不要净化你的字符串。使用参数化查询,而不是,因为他们处理所有的消毒。

Don't sanitize your strings. Use parameterized queries instead, as they handle all sanitization.

您没有指定的数据库使用的是的,所以我认为这是MS SQL Server的。微软<一href="http://www.asp.net/data-access/tutorials/using-parameterized-queries-with-the-sqldatasource-cs">an在有关本作的官方网站ASP.net文章。另请参见MSDN的<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx">SqlCommand.Parameters而<一href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx">AddWithValue方法。

You don't specify which database you are using, so I assume it is MS SQL Server. Microsoft has an article on the official ASP.net website about this. Also see MSDN for SqlCommand.Parameters and the AddWithValue method.

这篇关于我如何将字符串转换成安全的SQL字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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