将长SQL查询放在命令中有一些问题,但我总是遇到一些问题 - 你能帮我解决吗?将SQL查询放入带参数的字符串中
这是我的查询我想内cmd
把两个参数newvalue
和oldvalue
查询本身:
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'
和方法与CMD,其中上面的SQL应放在有两个参数:
Public Sub ChangeMachineNames(OldMachName As String, NewMachName As String)
Dim strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(System.String)).ToString()
Using con As New SqlConnection(strcon)
Using cmd As New SqlCommand("here i want to put my sql", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@newvalue", NewMachName)
cmd.Parameters.AddWithValue("@oldvalue", OldMachName)
con.Open()
Dim rowsAffected As Integer = cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
如果你解释你想要产生什么类型的查询,可能你可能会得到更多的答案,可能是解决你的问题的新方法。 – haraman