在 C# 中使用 sqlite 进行 SQL 转义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/633245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL escape with sqlite in C#
提问by
I have a text field and its breaking my sql statement. How do i escape all the chars in that field? I am using sqlite with http://sqlite.phxsoftware.com/in C#
我有一个文本字段,它破坏了我的 sql 语句。我如何转义该字段中的所有字符?我在 C# 中使用带有http://sqlite.phxsoftware.com/ 的sqlite
采纳答案by bstoney
You should be using a parameter as in:
您应该使用如下参数:
SQLiteCommand cmd = _connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM MyTable WHERE MyColumn = @parameter";
cmd.Parameters.Add( new SQLiteParameter( "@parameter", textfield ) );
SQLiteDataReader reader = cmd.ExecuteReader();
Using a parametrised SQL will escape all input values and help protect you from SQL injection attacks.
使用参数化 SQL 将转义所有输入值并帮助保护您免受 SQL 注入攻击。
回答by klkitchens
You can also replace all single quote delimiters with doubt single quotes (not ").
您还可以用怀疑单引号(不是“)替换所有单引号分隔符。
sql = sql.Replace("'","''");