简体   繁体   English

此asp.net代码不起作用

[英]This asp.net code is not working

Anyone know why the following is not working? 有人知道以下原因为何不起作用吗?

Dim strPhrase As String = "'%office%'"

objStringBuilder = New StringBuilder()

objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))

objSQLCommand = New SqlCommand("select col1, col2 from table1 where phrase like @phrase", objSQLConnection)

objSQLCommand.Parameters.Add("@phrase", SqlDbType.VarChar, 255).Value = strPhrase

objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()

While objSQLDataReader.Read()
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2"))
End While

objSQLDataReader.Close()
objSQLCommand.Connection.Close()

return objStringBuilder.tostring()

If I remove anything to do with phrase , it starts working again, ie: 如果我删除了与phrase任何内容,它将重新开始工作,即:

objStringBuilder = New StringBuilder()

objSQLConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connString"))

objSQLCommand = New SqlCommand("select col1, col2 from table1", objSQLConnection)

objSQLCommand.Connection.Open()
objSQLDataReader = objSQLCommand.ExecuteReader()

While objSQLDataReader.Read()
objStringBuilder.Append(objSQLDataReader("col1") & " - " & objSQLDataReader("col2"))
End While

objSQLDataReader.Close()
objSQLCommand.Connection.Close()

return objStringBuilder.tostring()

I get no errors, it just returns a blank string. 我没有收到任何错误,它只返回一个空白字符串。

Remove the '' from around your search phrase, the command object deals with all that for you. 从搜索短语中删除“”,命令对象将为您处理所有这一切。 ie

Dim strPhrase As String = "%office%"

you can make it like this 你可以这样

Dim strPhrase As String = "%office%"

and add bellow line to the end of your connection string 并将波纹管添加到连接字符串的末尾

allow user variables=true;

像这样

Dim strPhrase As String = "%office%"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM