2015-10-15 114 views
1

可能很简单的问题,但不想搞砸了。下面我查询:在sql串查询中添加额外的字符

Dim strCmd As String = "" 
strCmd &= " UPDATE tbElemPics" 
strCmd &= " SET PicturePath = Stuff(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)" 
strCmd &= " + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)" 
strCmd &= " + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1, @newvalue)" 
strCmd &= " WHERE Substring(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)" 
strCmd &= " + 1, Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)" 
strCmd &= " + 1) - Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1) = @oldvalue" 


Using cmd As New SqlCommand(strCmd, con) 

,现在我必须标记对此我路过那个地方= @oldvalue"N所以例如= N'mysomeoldvalue'字符串。我可以简单地做= [email protected]"吗?我必须使用它,因为特定的字符。希望你知道我的意思

+0

我强烈建议将此转换为存储过程,而不是像这样的一个巨大的SQL字符串。 –

回答

0

它已经作为NVarchar值传递,你不需要做任何特殊的事情。即:

Dim strCmd As String = <sql>UPDATE tbElemPics 
    SET PicturePath = Stuff(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1) + 1, 
    Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1) + 1) - 
    Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1, @newvalue) 
    WHERE Substring(PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)+ 1, 
    Charindex('/', PicturePath, Charindex('/', PicturePath, Charindex('/', PicturePath)+1)+ 1) - 
    Charindex('/', PicturePath, Charindex('/', PicturePath) + 1) - 1) = @oldvalue 
</sql> 
    Using con As New SqlConnection("server=.\sqlExpress;Database=Test;Trusted_Connection=yes") 
    Using cmd As New SqlCommand(strCmd, con) 
    cmd.Parameters.AddWithValue("@newValue", "My Unicode value") 
    cmd.Parameters.AddWithValue("@oldValue", "My Unicode value 2") 
    con.Open() 
    cmd.ExecuteNonQuery() 
    con.Close() 
    End Using 
    End Using 
+0

你在做什么是我的旧值是作为N'传递的,所以我不需要改变什么? – Arie

+0

是的。您可以使用Profiler(或者ExpressProfiler,如果您没有配置文件管理器)自己查看它。 –

相关问题