2015-10-15 104 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,如果您沒有配置文件管理器)自己查看它。 –

相關問題