如何在 VB.net 中为 SQL 编码字符串

发布于 2024-11-25 05:41:06 字数 180 浏览 0 评论 0原文

例如,在sql中

所有`应该替换为``,对吧?

那么,vb.net 是否已经内置了一个可以完成此类操作的函数?

这样我就不必对其进行编码。

顺便说一句,我不直接访问sql数据库。基本上我正在创建一个文本文件,该文本文件包含原始 sql 语句。大多数答案都涉及直接访问 sql 数据。

For example, in sql

all ` should be replaced with `` right?

Well, is there a function built in by vb.net that does that sort of thing already?

That way I do not have to encode it.

By the way, I do not access sql database directly. Basically I am creating a text file and that text file contains raw sql statements. Most of the answers deal with accessing sql data directly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-12-02 05:41:06

我不这么认为,因为我认为唯一与此相关的情况是您执行不带参数的内联 SQL 命令。

这存在 SQL 注入 的风险,因此您应该创建如下命令:

Dim cmd As New SqlCommand("UPDATE [TableA] SET ColumnA=@ColumnA WHERE ID=@ID", Conn)
cmd.Parameters.Add("@ColumnA", SqlDbType.NVarChar).Value = txtColumnA.Text
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID
cmd.ExecuteNonQuery()

I don't think so as I think the only case where something like this would be relevant is if you were doing inline SQL Commands without parameters.

This has a risk of SQL Injection, and therefore you should create commands like this:

Dim cmd As New SqlCommand("UPDATE [TableA] SET ColumnA=@ColumnA WHERE ID=@ID", Conn)
cmd.Parameters.Add("@ColumnA", SqlDbType.NVarChar).Value = txtColumnA.Text
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID
cmd.ExecuteNonQuery()
漆黑的白昼 2024-12-02 05:41:06

不要尝试这样做!我知道您正在努力避免 SQL 注入,因此您对安全性的考虑值得赞扬。然而,自行执行消毒程序很容易出错。

在查询中使用以下参数

cmd.CommandText = "select * from customer where id=?id";

cmd.Parameters.AddWithValue("?id",CustomerIDValue);

Dont try and do this! I know you are trying to avoid SQL Injection so you are to be commended for thinking about security. However rolling your own sanitisation routine is something that is easy to get wrong.

Use parameters in your query along the lines of

cmd.CommandText = "select * from customer where id=?id";

cmd.Parameters.AddWithValue("?id",CustomerIDValue);
伪装你 2024-12-02 05:41:06

如果您使用字符串,那么您将在代码中使用",这样您就不需要转义这些字符。

Dim mySql As String = "SELECT `MyColumn` FROM `Table`"

If you are using a string then you'll be using " in your code so you won't need to escape these characters.

Dim mySql As String = "SELECT `MyColumn` FROM `Table`"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文