在SQL查询中转义值(C#与SQL连接器) [英] Escaping values in SQL queries (C# with SQL connector)

查看:106
本文介绍了在SQL查询中转义值(C#与SQL连接器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用参数,但是什么是正确的方法来转义字符串序列?
查询可能是这样的:

 INSERT INTO记录(ReferenceID,Name,Note,Author)VALUES '+ ID +','+ addlevel.textBox1.Text +','+ addlevel.textBox2_note.Text +','+ Program.Username +'); 

我很好奇,只想知道:)



编辑:
但是呢?
CREATE TABLE+ string....参数不能在这里使用

解决方案

如果您需要执行数据库操作,例如创建表,那么您应该使用 SQL Server管理对象而不是执行SQL字符串。



对于CRUD操作参数绝对是唯一真正的路径。



更新:看来,MySQL客户端库包含一个这个不明智的任务的帮助方法,你可以调用MySqlHelper.EscapeString(string)。


I know I can use the parameters, but what is the right way to escape string sequences? The query could be like this:

"INSERT INTO records (ReferenceID,Name,Note,Author) VALUES ('" + ID+ "','" + addlevel.textBox1.Text + "','"+addlevel.textBox2_note.Text+ "','"+Program.Username+"')";

I am ONLY curious, just want to know :)

EDIT: But what about that? "CREATE TABLE "+string" .... parameters cannot be used here!

解决方案

If you need to perform database operations, such as creating tables, then you should use SQL Server Management Objects instead of executing SQL strings.

For CRUD operations parameters is absolutely the only true path.

UPDATE: It appears that the MySQL client library contains a helper method for this ill-advised task. You can call MySqlHelper.EscapeString(string).

这篇关于在SQL查询中转义值(C#与SQL连接器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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