从SQL中选择此列,如标签C# [英] Select from SQL here column like label C#

查看:74
本文介绍了从SQL中选择此列,如标签C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有这部分工作正常



Without this part work fine

and Radnik like '" + notificationLabel.Text + "' ";





我错了什么?



将代码放入sql





What I wrong?

When put code in sql

select datumpodnosenja as 'Datum', radnik as 'Radnik', status as 'Status' from dbo.izlaznice where tip='Izlaznica'and Radnik like '%' <pre><pre>





正常结果



一些帮助



我的尝试:





Have normal result

Some help

What I have tried:

String saveStaff = " select datumpodnosenja as 'Datum', radnik as 'Radnik', status as 'Status' from dbo.izlaznice where tip='Izlaznica' and Radnik like '" + notificationLabel.Text + "' ";

推荐答案

对于初学者,不要这样做!永远不要连接字符串来构建SQL命令。它会让你对意外事件或故意的SQL注入攻击wh ich可以破坏你的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

For starters, don't do that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?



然后你可以看看为什么它不起作用 - 因为那是你给我们的所有信息我必须猜测这意味着什么。

可能是因为没有SQL通配符的SQL LIKE与SQL =

相同这意味着如果您的标签文本包含ABC 那么你的LIKE只返回列与ABC完全匹配的行。如果你想要通配符比较,你必须指定通配符!

试试这个:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then you can look at why it "doesn't work" - and since that's all the info you give us I'll have to guess what that means.
Probably, it's because SQL LIKE without SQL wildcards is identical to SQL =
Which means that if your label text contains "ABC" then your LIKE will only return rows where the column exactly matches "ABC". If you want wildcard comparisons, you have to specify the wildcards!
Try this:

AND Radnik LIKE '%' + @NL + '%'

并将 notificationLabel.Text 作为参数传递给 @NL


这取决于你想要发生什么以及notificationLabel.Text中的内容。如果你想要Radnik包含文本的任何内容,那么你需要的语法是



WHERE Radnik LIKE'%my text%'



因此,如果notificationLabel.Text包含我的文本,那么您的代码需要;



It depends what you want to happen and what is in notificationLabel.Text. If you want anything where Radnik contains the text then the syntax you need is

WHERE Radnik LIKE '%my text%'

So if notificationLabel.Text contains "my text" then your code needs to be;

String saveStaff = " select datumpodnosenja as 'Datum', radnik as 'Radnik', status as 'Status' from dbo.izlaznice where tip='Izlaznica' and Radnik like '%" + notificationLabel.Text + "%' ";





但是你应该使用参数化查询而不是使用字符串连接来构建查询。如果notificationLabel.Text包含撇号,您的代码将失败,但更严重的是它可能会使您的代码容易受到SQL注入攻击。



However you should be using parameterised queries rather than using string concatenation to build your queries. Your code will fail if notificationLabel.Text contains an apostrophe, but more seriously it might make your code liable to SQL injection attacks.


这篇关于从SQL中选择此列,如标签C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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