如何在数据库中查找和更新路径 [英] How to find and update the path in database
问题描述
亲爱的朋友
i在查询/逻辑中有问题
我保存文件的完整路径说
c:\ mysyncfolder \ hello \ abc.txt
c:\ mysyncfolder \ hello123 \ abc1.txt
以防用户重命名文件夹说'hello'到'hello1'
i想要更新数据库中的路径
但是它还返回文件夹hello123。你好文件夹
如何解决这个问题
我尝试过:
目前iam使用以下查询
从FileInformation选择*其中FilePath类似'+ myPath +%'和状态<>'已删除'
dear friends
i have a problem in the query/logic
iam saving the complete path of my files say
c:\mysyncfolder\hello\abc.txt
c:\mysyncfolder\hello123\abc1.txt
in case the user is renaming the folder say 'hello' to 'hello1'
i want to update the path in the database
but it is also returning the folders hello123. with hello folder
how can i solve this problem
What I have tried:
currently iam using following query
Select * from FileInformation Where FilePath like '" + myPath + "%' And Status<>'Deleted'
推荐答案
如果您只想选择整个文件夹名称,则使用显式反斜杠:
If you are trying to select "whole folder names" only, then use explicit backslashes:
SELECT * FROM FileInformation Where FilePath LIKE '" + myPath.Trim('\\') + "\\%'" AND Status <> 'Deleted'
但请不要这样做。
不要连接字符串来构建一个SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
But please, don't do it like that.
Do not 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. Use Parametrized queries instead.
我能想到的两种方式:
使用'_'通配符。它是一个单独的字符通配符,不像'%',它是0到多个字符。
或者你可以寻找最接近的匹配,你的情况将是最短的filepath。
你将无法区分hello1.txt和helloz.txt
Two ways I can think of:
Use the '_' wildcard. It is a single char wildcard unlike '%' which is 0 to many chars.
Or you can look for the closest match, which it your case will be the shortest filepath.
You won't be able to tell the difference between hello1.txt and helloz.txt
这篇关于如何在数据库中查找和更新路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!