在下面的代码中,SqlDataReader“dr”不读取名称中包含空格的主题的“SubjectCode”;主题表中的英语语言,社会研究,成本会计原理等。
如果有人能够立刻帮助我,我会非常感激。
cmd = New SqlCommand( _
"select subjectCode from ProgramDetails.subjects where subjectname='" & _
Trim(subname) & " ' ", cn)
dr = cmd.ExecuteReader
If dr.Read Then
subcode = dr(0)
End If
dr.Close()
答案 0 :(得分:3)
SQL-Parameters
LIKE
使用通配符%
代替:例如:
Dim sql = "Select subjectCode From ProgramDetails.subjects where subjectname Like @subname"
Using con = New SqlConnection(connectionString)
Using cmd = New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@subname", String.Format("%{0}%", "english"))
Con.Open()
Using rd = cmd.ExecuteReader()
While rd.Read()
Dim subjectCode = rd.GetString(0)
' ... '
End While
End Using
End Using
End Using
答案 1 :(得分:0)
只需使用RTRIM
即可 cmd = New SqlCommand("select RTRIM(subjectCode) from ProgramDetails.subjects " + _
"where subjectname Like '" & Trim(subname) & "%'", cn)
dr = cmd.ExecuteReader
If dr.Read Then
subcode = dr(0)
End If
dr.Close()